現在大多數的APP都需要第三方登錄,今天簡單的寫下通過友盟來實現第三方登錄步驟
1 首先是申請UMkey 根據UM的文檔導數SDK 配置庫文件(不作說明了)
2 申請要連接的第三方Key (QQ 微信 新浪等)
配置時要注意
0EA14743-2B44-4F72-87A4-CAFA00A47E06.png
記得加入白名單
CEBD765D-4F41-4129-837A-C110E2038869.png
3 代碼方面
3.1 AppDelegate.m 里
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
-(void)loadUM
{
[UMSocialData setAppKey:UMKey];
//設置微信AppId、appSecret,分享url
[UMSocialWechatHandler setWXAppId:WXKey appSecret:WXSercret url:@"http://www.umeng.com/social"];
//設置手機QQ 的AppId,Appkey,和分享URL,需要#import "UMSocialQQHandler.h"
[UMSocialQQHandler setQQWithAppId:QQId appKey:QQKey url:@"http://www.umeng.com/social"];//
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
//return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
BOOL result = [UMSocialSnsService handleOpenURL:url];
if (result == FALSE) {
//調用其他SDK,例如支付寶SDK等
}
return result;
}
3.2 登錄界面里
// QQ登錄
- (IBAction)qq:(id)sender {
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
// 獲取微博用戶名、uid、token等
NSLog(@"%@",response);
if (response.responseCode == UMSResponseCodeSuccess) {
NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
// NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
NSDictionary*qq = [response.data objectForKey:@"qq"];
//下面是我向服務器請求
UserOtherLoginClientEntity*client = [[UserOtherLoginClientEntity alloc]init];
client.nikename = snsAccount.userName; //qq昵稱
client.headimg = snsAccount.iconURL;//qq頭像
client.openkey = [qq objectForKey:@"usid"]; //qq唯一標識
[[Network new] httpRequestJosn:client WithRespoinseObj:^(id respoinseObj) {
Block:^(NSHTTPURLResponse *responseObj) {
}];
}});
}
//微信登錄
- (IBAction)weixin:(id)sender {
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
NSLog(@"%@",response);
if (response.responseCode == UMSResponseCodeSuccess) {
NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
// NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
UserOtherLoginClientEntity*client = [[UserOtherLoginClientEntity alloc]init];
client.nikename = snsAccount.userName;
client.headimg = snsAccount.iconURL;
client.openkey = [response.thirdPlatformUserProfile objectForKey:@"openid"];
[[Network new] httpRequestJosn:client WithRespoinseObj:^(id respoinseObj) {
NSLog(@"%@",respoinseObj);
if ([[respoinseObj objectForKey:@"status"]isEqualToString:@"2000106"]) {
}
} failBlock:^(NSHTTPURLResponse *responseObj) {
}];
}
});
}