iOS整合友盟第三方登入
阿新 • • 發佈:2019-02-08
可以參考demo來做,demo是可以跑起來的,如果用demo,只需要把key和ID換掉就好了,點選下載demo
註冊APPKey
匯入SDK
匯入友盟提供的SDK,其目錄結構如下
只需要將UMSocial匯入專案中,接下來配置SDK
配置
在Build Settings
->Other Linker Flags
加入-ObjC
如圖所示
設定Web支援HTTP
因為iOS目前不支援HTTP,所以在web登入中還需要配置HTTP,配置方法如下:
在Info-plist
檔案中新增App Transport Security Settings
其型別為Dictionary
App Transport Security Settings
中新增Allow Arbitrary Loads
,其型別為BOOL,將其修改為YES
配置成功如下:
新增依賴庫
友盟依賴庫
- libsqlite3.tbd
- CoreGraphics.framework
微信依賴庫
- SystemConfiguration.framework
- CoreTelephony.framework
- libsqlite3.tbd
- libc++.tbd
- libz.tbd
- QQ依賴庫
- SystemConfiguration.framework
- libc++.tbd
- 微博依賴庫
- SystemConfiguration.framework
- CoreTelephony.framework
- ImageIO.framework
- libsqlite3.tbd
- libz.tbd
匯入的庫如圖:
設定白名單&Schemes
- 設定SSO白名單
配置白名單小技巧,右擊Info.plist檔案,選擇文字編輯器開啟,將下面程式碼直接copy進去,沒必要為這些東西浪費時間
<key>LSApplicationQueriesSchemes</key>
<array>
<!-- 微信 URL Scheme 白名單-->
<string >wechat</string>
<string>weixin</string>
<!-- 新浪微博 URL Scheme 白名單-->
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<!-- QQ、Qzone URL Scheme 白名單-->
<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>mqqopensdkapiV4</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>
- 配置Schemes
來到Info
->URL Types
配置Schemes
平臺 | APPKey | 舉例 | 注意 |
---|---|---|---|
100424468 | tencent100424468 | QQ需要新增tencent | |
微信 | wxdc1e388c3822c80b | wxdc1e388c3822c80b | 微信直接用 |
微博 | 3921700954 | wb3921700954 | 微博新增wb |
配置如圖所示:
使用
註冊平臺
匯入SDK標頭檔案#import <UMSocialCore/UMSocialCore.h>
在AppDelegate.m檔案中配置,在application:application didFinishLaunchingWithOptions:
方法中新增
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中註冊友盟的AppKey
//設定AppKey,是在友盟註冊之後給到的key
[[UMSocialManager defaultManager] setUmSocialAppkey:UMKEY];
然後註冊各個平臺的AppKey和AppID
// setPlaform是要註冊的平臺
/* 微信 */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxdc1e388c3822c80b" appSecret:@"3baf1193c85774b3fd9d18447d76cab0" redirectURL:@"http://mobile.umeng.com/social"];
/*QQ*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1106152098" appSecret:@"KexLaOiU6VVzxgQb" redirectURL:@"http://mobile.umeng.com/social"];
/*
注**
redirectURL必須要和微博設定的回撥一致
*/
/* 新浪 */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"1234957746" appSecret:@"b3564b393bfcb42672d8e84f084ea550" redirectURL:@"https://sns.whalecloud.com/sina2/callback"];
設定回撥
在AppDelegate.m中新增以下方法設定回撥
// 支援所有iOS系統版本回調
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if (!result) {
// 其他如支付等SDK的回撥
}
return result;
}
使用
匯入SDK標頭檔案#import <UMSocialCore/UMSocialCore.h>
* QQ授權登入
- (void)QQLogin{
/**
設定QQ授權登入
@param platformType 平臺
@param result 授權成功
@param error 授權失敗
*/
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授權資訊
NSLog(@"QQ uid: %@", resp.uid);
NSLog(@"QQ openid: %@", resp.openid);
NSLog(@"QQ accessToken: %@", resp.accessToken);
NSLog(@"QQ expiration: %@", resp.expiration);
// 使用者資訊
NSLog(@"QQ name: %@", resp.name);
NSLog(@"QQ iconurl: %@", resp.iconurl);
NSLog(@"QQ gender: %@", resp.gender);
// 第三方平臺SDK源資料
NSLog(@"QQ originalResponse: %@", resp.originalResponse);
}
}];
}
- 微信登入
/** 微信授權登入 */
- (void)WeChatLogin{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
} else {
UMSocialUserInfoResponse *resp = result;
// 授權資訊
NSLog(@"Wechat uid: %@", resp.uid);
NSLog(@"Wechat openid: %@", resp.openid);
NSLog(@"Wechat accessToken: %@", resp.accessToken);
NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
NSLog(@"Wechat expiration: %@", resp.expiration);
// 使用者資訊
NSLog(@"Wechat name: %@", resp.name);
NSLog(@"Wechat iconurl: %@", resp.iconurl);
NSLog(@"Wechat gender: %@", resp.gender);
// 第三方平臺SDK源資料
NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
}
}];
}
- 新浪微博登入
/** 新浪授權登入 */
- (void)SinaLogin{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:self completion:^(id result, NSError *error) {
UMSocialUserInfoResponse *resp = result;
// 第三方登入資料(為空表示平臺未提供)
// 授權資料
NSLog(@" uid: %@", resp.uid);
NSLog(@" openid: %@", resp.openid);
NSLog(@" accessToken: %@", resp.accessToken);
NSLog(@" refreshToken: %@", resp.refreshToken);
NSLog(@" expiration: %@", resp.expiration);
// 使用者資料
NSLog(@" name: %@", resp.name);
NSLog(@" iconurl: %@", resp.iconurl);
NSLog(@" gender: %@", resp.gender);
// 第三方平臺SDK原始資料
NSLog(@" originalResponse: %@", resp.originalResponse);
}];
}