本文主要介紹新浪微博,QQ,微信的登陸接入以及如何配合BmobSDK中的第三方登陸功能實現第三方登陸。html
在使用以前請先按照快速入門建立好能夠調用BmobSDK的工程。ios
1.下載新浪SDK,並按照上面給的文檔說明,在新浪的後臺建立應用並配置好工程。git
2.在AppDelegate中實現回調。github
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate,WeiboSDKDelegate>
AppDelegate.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [WeiboSDK handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [WeiboSDK handleOpenURL:url delegate:self];
}
3.請求受權信息,可在點擊登陸處實現web
//向新浪發送請求
WBAuthorizeRequest *request = [WBAuthorizeRequest request];
request.redirectURI = @"https://api.weibo.com/oauth2/default.html";
request.scope = @"all";
[WeiboSDK sendRequest:request];
4.接收回調信息並與Bmob帳號進行綁定,首次登陸時Bmob後臺會建立一個帳號。api
NSString *accessToken = [(WBAuthorizeResponse *)response accessToken];
NSString *uid = [(WBAuthorizeResponse *)response userID];
NSDate *expiresDate = [(WBAuthorizeResponse *)response expirationDate];
NSLog(@"acessToken:%@",accessToken);
NSLog(@"UserId:%@",uid);
NSLog(@"expiresDate:%@",expiresDate);
NSDictionary *dic = @{@"access_token":accessToken,@"uid":uid,@"expirationDate":expiresDate};
//經過受權信息註冊登陸
[BmobUser loginInBackgroundWithAuthorDictionary:dic platform:BmobSNSPlatformSinaWeibo block:^(BmobUser *user, NSError *error) {
if (error) {
NSLog(@"weibo login error:%@",error);
} else if (user){
NSLog(@"user objectid is :%@",user.objectId);
}
}];
1.進入騰訊開放平臺註冊用戶,建立應用(須要審覈);微信
2.按照開發文檔導入SDK,而後把註冊成功後獲取到的Key加入到Url Schemes中,格式爲:tencentXXXX;app
3.在AppDelegate.m中實現下面方法async
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [TencentOAuth HandleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [TencentOAuth HandleOpenURL:url];
}
4.註冊並實現受權ui
//註冊
_oauth = [[TencentOAuth alloc] initWithAppId:@"1104720526" andDelegate:self];
//受權
NSArray *permissions = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_INFO,nil];
[_oauth authorize:permissions inSafari:NO];
//獲取用戶信息
[_oauth getUserInfo];
5.獲取AccessToken等信息,此處爲實現TencentSessionDelegate中的方法,並進行綁定。
- (void)tencentDidLogin{
if (_tencentOAuth.accessToken && 0 != [_tencentOAuth.accessToken length]){
// 記錄登陸用戶的OpenID、Token以及過時時間
NSString *accessToken = _tencentOAuth.accessToken;
NSString *uid = _tencentOAuth.openId;
NSDate *expiresDate = _tencentOAuth.expirationDate;
NSLog(@"acessToken:%@",accessToken);
NSLog(@"UserId:%@",uid);
NSLog(@"expiresDate:%@",expiresDate);
NSDictionary *dic = @{@"access_token":accessToken,@"uid":uid,@"expirationDate":expiresDate};
//經過受權信息註冊登陸
[BmobUser loginInBackgroundWithAuthorDictionary:dic platform:BmobSNSPlatformQQ block:^(BmobUser *user, NSError *error) {
if (error) {
NSLog(@"weibo login error:%@",error);
} else if (user){
NSLog(@"user objectid is :%@",user.objectId);
//跳轉
ShowUserMessageViewController *showUser = [[ShowUserMessageViewController alloc] init];
showUser.title = @"用戶信息";
[self.navigationController pushViewController:showUser animated:YES];
}
}];
}
}
- (void)tencentDidNotLogin:(BOOL)cancelled{
}
- (void)tencentDidNotNetWork{
}
1.到微信開放平臺註冊帳號並提交應用審覈;
2.按照官方文檔配置好SDK,導入相應的依賴包,添加URL scheme;
3.在AppDelegate實現下面方法;
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [TencentOAuth HandleOpenURL:url] ||
[WeiboSDK handleOpenURL:url delegate:self] ||
[WXApi handleOpenURL:url delegate:self];;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [TencentOAuth HandleOpenURL:url] ||
[WeiboSDK handleOpenURL:url delegate:self] ||
[WXApi handleOpenURL:url delegate:self];;
}
4.實現點擊發送受權請求
- (IBAction)weixinLogin:(id)sender {
SendAuthReq* req =[[SendAuthReq alloc ] init];
req.scope = @"snsapi_userinfo,snsapi_base";
req.state = @"0744" ;
[WXApi sendReq:req];
}
5.發送受權後到完成綁定須要通過兩步。
1)獲取code
2)利用code獲取token,openId和expiresDate
代碼在AppDelegate.m中實現。以下:
-(void)onResp:(BaseReq *)resp
{
/* ErrCode ERR_OK = 0(用戶贊成) ERR_AUTH_DENIED = -4(用戶拒絕受權) ERR_USER_CANCEL = -2(用戶取消) code 用戶換取access_token的code,僅在ErrCode爲0時有效 state 第三方程序發送時用來標識其請求的惟一性的標誌,由第三方程序調用sendReq時傳入,由微信終端回傳,state字符串長度不能超過1K lang 微信客戶端當前語言 country 微信用戶當前國家信息 */
SendAuthResp *aresp = (SendAuthResp *)resp;
if (aresp.errCode== 0) {
NSString *code = aresp.code;
[self getAccessToken:code];
}
}
-(void)getAccessToken:(NSString*)code{
//https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
#warning 在此處須要填寫你自身的appid和secretkey
NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",@"填入你的appid",@"填入你的secretkey",code];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:url];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
NSDictionary *dicFromWeixin = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
/* { "access_token" = "OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN7Kaf3e4GZ2OncrCfiKnGWiusJMZwzQU8kXcnT1hNs_ykAFDfDEuNp6waj-bDdepEzooL_k1vb7EQzhP8plTbD0AgR8zCRi1It3eNS7yRyd5A"; "expires_in" = 7200; openid = oyAaTjsDx7pl4Q42O3sDzDtA7gZs; "refresh_token" = "OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN7Kaf3e4GZ2OncrCfiKnGWi2ZzH_XfVVxZbmha9oSFnKAhFsS0iyARkXCa7zPu4MqVRdwyb8J16V8cWw7oNIff0l-5F-4-GJwD8MopmjHXKiA"; scope = "snsapi_userinfo,snsapi_base"; } */
// 記錄登陸用戶的OpenID、Token以及過時時間
NSString *accessToken = [dicFromWeixin objectForKey:@"access_token"];
NSString *uid = [dicFromWeixin objectForKey:@"openid"];
NSNumber *expires_in = [dicFromWeixin objectForKey:@"expires_in"];
NSDate *expiresDate = [NSDate dateWithTimeIntervalSinceNow:[expires_in doubleValue]];
NSLog(@"acessToken:%@",accessToken);
NSLog(@"UserId:%@",uid);
NSLog(@"expiresDate:%@",expiresDate);
NSDictionary *dic = @{@"access_token":accessToken,@"uid":uid,@"expirationDate":expiresDate};
//經過受權信息註冊登陸
[BmobUser loginInBackgroundWithAuthorDictionary:dic platform:BmobSNSPlatformWeiXin block:^(BmobUser *user, NSError *error) {
if (error) {
NSLog(@"weibo login error:%@",error);
} else if (user){
NSLog(@"user objectid is :%@",user.objectId);
}
}];
}
});
});
}