使用友盟來實(shí)現(xiàn)第三方登錄,但是需要在新浪微博登錄的時(shí)候添加關(guān)注官微的功能(下文簡稱為浪微),通過給友盟和新浪發(fā)郵件最終在Google找到了答案
說明:浪微屬于scope功能锡宋,如果只是用第三方的登錄和簡單的分享功能一般用不到父虑。所以如果要使用,大部分需要進(jìn)一步申請碍现,而浪微只需要在新浪開放平臺的控制臺填好官微就可以。
示例
- 在登錄界面做判斷米奸,如果是新浪微博登錄昼接,那就不使用友盟的,而是用新浪自己的SDK
if (platformName == UMSocialPlatformType_Sina) {
//設(shè)置請求參數(shù)
WBAuthorizeRequest *request = [WBAuthorizeRequest request];
//scope可以拼接多個(gè)參數(shù)悴晰,以逗號分割(我只用到了浪微)
request.scope = @"follow_app_official_microblog";
//回調(diào)的URL
request.redirectURI = SINA_REDIRECT_URL;
request.shouldShowWebViewForAuthIfCannotSSO = NO;
//跳轉(zhuǎn)到微博
[WeiboSDK sendRequest:request];
} else {//其他第三方平臺:微信慢睡、QQ等
[[UMSocialManager defaultManager] getUserInfoWithPlatform:platformName currentViewController:self completion:^(id result, NSError *error) {
}]
}
- 在AppDelegate中設(shè)置代理
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[WeiboSDK handleOpenURL:url delegate:self]; //設(shè)置代理
}
- 接著實(shí)現(xiàn)代理方法
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
//我這里只用了登錄返回,分享還是用的友盟铡溪,所以只要不是登錄授權(quán)返回就不處理
if (![response isKindOfClass:[WBAuthorizeResponse class]]) {
return;
}
WBAuthorizeResponse *resp = (WBAuthorizeResponse *)response;
if ([response valueForKey:@"accessToken"] == nil) {
return;
}
//返回值只有一個(gè)uid,所以需要我們用另一個(gè)接口請求該uid對應(yīng)用戶的所有信息
NSString *api = @"https://api.weibo.com/2/users/show.json";
NSMutableDictionary *parametersDic = [NSMutableDictionary new];
[parametersDic setObject:resp.accessToken forKey:@"access_token"];
[parametersDic setObject:resp.userID forKey:@"uid"];
[[DMHttpRequestManager sharedManager] get:api withParameters:parametersDic success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject valueForKey:@"name"] == nil) {
return ;
} else {
//獲取到數(shù)據(jù)漂辐,以通知的形式傳值到登錄界面
[[NSNotificationCenter defaultCenter] postNotificationName:@"WBLOGIN" object:responseObject];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
- 為了接受到通知,需要在登錄界面注冊該通知棕硫,然后處理用戶信息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weiboLogin:) name:@"WBLOGIN" object:nil];
代碼到此結(jié)束髓涯,但是為什么登錄界面還是沒有更多權(quán)限顯示呢?哈扮?纬纪?
你把賬號對官微的關(guān)注取消了再試試!;狻育八!
我在這里糾結(jié)了好久,然后才Google到的答案赦邻,切記髓棋、切記!