在开发过程中,常常会遇到第三方授权登录,这里用友盟比较快捷方便
1.导入友盟的SDK,当然用CocoaPods更加方便快捷
pod 'UMengSocial'
2.设置友盟appkey
- 获取友盟Appkey。如果你之前已经在友盟注册了应用,获得了Appkey,可以继续使用之前获得Appkey。
- 如果你尚未在友盟注册账号,需要先注册,注册之后登录你的账号,点击添加新应用,完成新应用填写之后,将进入应用管理页面。在该页面就能得到Appkey.
- 在代码中设置你的友盟Appkey,在AppDelegate
文件内设置你的AppKey:
#import "UMSocial.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[UMSocialData setAppKey:@"507fcab25270157b37000010"];
}
3.微信登录
- 使用微信分享首先需要在微信开放平台(点击链接)申请,按照要求填写应用信息,审核通过后获取到微信APPID和APPsecret
在你的程序APPdelegate入口方法添加下面的代码
#import "UMSocialWechatHandler.h"
//设置微信AppId、appSecret,分享url
[UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f"
appSecret:@"db426a9829e4b49a0dcac7b4162da6b6"
url:@"http://www.umeng.com/social"];
- 在微信登录按钮中实现下面的方法
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
}
});
-
当然还要去Info.plist中设置 URL schemes
在你的工程设置项,targets 一栏下,选中自己的 target,在 Info->URL Types 中添加 URL Schemes。如果使用的是Xcode3或更低版本,则需要在plist文件中添加。获取各个平台appkey或者appid的方法可以参考分享详细文档绑定各个平台部分,各个平台的url scheme格式如下表:
屏幕快照 2016-04-15 上午11.44.49.png
- 之后还要去设置白名单
<key>LSApplicationQueriesSchemes</key>
<array>
<string>iosamap</string>
<string>alipay</string>
<string>alipayshare</string>
<string>wechat</string>
<string>weixin</string>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqqbrowser</string>
<string>mttbrowser</string>
</array>
这个就是微信的登录方法,在登录后就会回调回你所需要的信息.
4.微博登录
- 在你的工程设置项,targets 一栏下,选中自己的 target,在 Info->URL Types 中添加 URL Schemes,格式为“wb”+新浪appkey,例如“wb126663232”
- 配置微博APPkey
#import "UMSocialSinaSSOHandler.h"
//第一个参数为新浪appkey,第二个参数为新浪secret,第三个参数是新浪微博回调地址,这里必须要和你在新浪微博后台设置的回调地址一致。
[UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"3921700954"
secret:@"04b48b094faeb16683c32669824ebdad"
RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
/**
这里处理新浪微博SSO授权之后跳转回来,和微信分享完成之后跳转回来
*/
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}
/**
这里处理新浪微博SSO授权进入新浪微博客户端后进入后台,再返回原来应用
*/
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[UMSocialSnsService applicationDidBecomeActive];
}
- 删除授权调用下面的方法
[[UMSocialDataService defaultDataService]
requestUnOauthWithType:UMShareToSina completion:^(UMSocialResponseEntity *response){
NSLog(@"response is %@",response);
}];
4.QQ及Qzone登录
QQ登录只支持SSO登录方式,必须具备手机QQ客户端,Qzone默认调用SSO登录
添加配置文件参考文档:添加QQ,添加相关库文件,配置URL schemes及添加系统回调
在AppDelegate应用入口方法添加下面的方法
//设置分享到QQ的应用Id,和分享url 链接
#import "UMSocialQQHandler.h"
[UMSocialQQHandler setQQWithAppId:@"100424468"
appKey:@"c7394704798a158208a74ab60104f0ba"
url:@"http://www.umeng.com/social"];
在QQ登录按钮中实现下面的方法
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
// 获取微博用户名、uid、token等
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
}});
如果还有哪里不明白,可以直接去友盟官方文档查看













网友评论