现在大多数的APP都需要第三方登录,今天简单的写下通过友盟来实现第三方登录步骤
1 首先是申请UMkey 根据UM的文档导数SDK 配置库文件(不作说明了)
2 申请要连接的第三方Key (QQ 微信 新浪等)
配置时要注意
记得加入白名单
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) {
}];
}
});
}