为WBShareKit增加程序内登陆认证(UIWebView)
WBShareKit是什么
WBShareKit是一个支持新浪微博,腾讯微博,豆瓣说,发推,网易微博等登陆,发布的开源库,认证方式使用的oauth。
官方地址是:WBShareKit
个人觉得WBShareKit是国内鲜有的优秀开源库之一。在微博大行其道的今天,很少有人将这么多的微博登陆封装到一块,当然不排除有这样的牛人,估计大多也都是有开源的心,没有开源的勇气。笔者很庆幸在接到程序内发微博的任务的时候遇到了WBShareKit,你不知道这样节省了我多少的时间。
WBShareKit用了挺久了,很稳定用着也很顺手,唯一觉得不大爽的地方就是用户登陆的时候,都是用的系统自带的safari实现的。这样的话,如果用户登陆不成功,会造成一定数量的用户流失。笔者当时也想过给WBShareKit加上程序内登陆的功能,无耐功力有限,觉得无从下手。当头两天这个任务再次被提上来了,竟然用了几个小时的时间给搞定了。不敢独享特整理出来,方便更多有这样需求的人。
主要改动:
WBShareKit.h 增加了一个UIWebViewDelegate,以及一个View
@interface WBShareKit : NSObject<UIWebViewDelegate>{ SEL _successSEL; SEL _failSEL; } @property (nonatomic, retain) UIView *safariView;
WBShareKit.m 系统方面,初始化并销毁新创建的view
+ (WBShareKit *)mainShare{ if (nil == _shareKit) { _shareKit = [[WBShareKit alloc] init]; _shareKit.safariView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; } return _shareKit; } - (void)dealloc{ [safariView release]; [super dealloc]; }
以下是大段的新添加的代码,直接添加到了WBShareKit.m文件的最下方
#pragma mark - safariView 弹出动画 #define kTransitionDuration 0.3 - (CGAffineTransform)transformForOrientation { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (orientation == UIInterfaceOrientationLandscapeLeft) { return CGAffineTransformMakeRotation(M_PI*1.5); } else if (orientation == UIInterfaceOrientationLandscapeRight) { return CGAffineTransformMakeRotation(M_PI/2); } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { return CGAffineTransformMakeRotation(-M_PI); } else { return CGAffineTransformIdentity; } } - (void)bounce1AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)]; safariView.transform = CGAffineTransformScale([self transformForOrientation], 0.9, 0.9); [UIView commitAnimations]; } - (void)bounce2AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; safariView.transform = [self transformForOrientation]; [UIView commitAnimations]; } #pragma mark - 显示safariView 程序内登陆的界面 - (void)doShowSafariWebView:(NSString *)_url{ if ([safariView.subviews count] == 0) { //添加一个头 UIButton *titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [titleBtn setBackgroundImage:[UIImage imageNamed:@"head"] forState:UIControlStateHighlighted]; [titleBtn setBackgroundImage:[UIImage imageNamed:@"head"] forState:UIControlStateNormal]; [safariView addSubview:titleBtn]; [titleBtn release]; UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 45)]; [backBtn setTitle:@"取消" forState:UIControlStateNormal]; backBtn.titleLabel.font = [UIFont systemFontOfSize:14]; backBtn.titleLabel.adjustsFontSizeToFitWidth = TRUE; [backBtn addTarget:self action:@selector(removeSafariview) forControlEvents:UIControlEventTouchUpInside]; [backBtn setBackgroundImage:[UIImage imageNamed:@"bianji.png"] forState:UIControlStateNormal]; [backBtn setBackgroundImage:[UIImage imageNamed:@"bianjidown.png"] forState:UIControlStateHighlighted]; [safariView addSubview:backBtn]; [backBtn release]; UIWebView *_curWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 416)]; _curWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _curWebView.delegate = self; [_curWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]]; _curWebView.tag = 5; [safariView addSubview:_curWebView]; [_curWebView release]; } else { UIView *_curWebView = [safariView viewWithTag:5]; [(UIWebView *)_curWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]]; } UIWindow *window = [UIApplication sharedApplication].keyWindow; if (!window) { window = [[UIApplication sharedApplication].windows objectAtIndex:0]; } [window addSubview:safariView]; safariView.frame = CGRectMake(0, 20, 320, 460); safariView.transform = CGAffineTransformScale([self transformForOrientation], 0.001, 0.001); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/1.5]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)]; safariView.transform = CGAffineTransformScale([self transformForOrientation], 1.1, 1.1); [UIView commitAnimations]; } - (void)removeSafariview{ [safariView removeFromSuperview]; UIView *_curWebView = [safariView viewWithTag:5]; [(UIWebView *)_curWebView loadHTMLString:@"" baseURL:nil]; } #pragma mark - UIWebViewDelegate - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ if ([[request.URL absoluteString] hasPrefix:CallBackURL]) { [self handleOpenURL:request.URL]; [self removeSafariview]; return NO; } else { return YES; } }
在WBShareKit.m中间做的操作就是把
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
替换为:
[self performSelector:@selector(doShowSafariWebView:) withObject:url];
然后就是添加了三个图片文件,that’s all
我是今天新下载的代码:
原始代码 修改后的代码
最后再次对WBShareKit作者的开源精神表示崇高的敬意!开源万岁!