1.第三方賬號申請
微博: AppID AppKey AppSecret
微信: AppID及AppSecret(有效期為一年)
QQ&空間:AppID及AppKey
2.快速集成文檔
使用CocoaPods
target 'UMSocialDemo' do
# U-Share SDK UI模塊(分享面板,建議添加)
pod ‘UMengUShare/UI’
# 集成新浪微博
pod ‘UMengUShare/Social/Sina'
# 集成微信
pod ‘UMengUShare/Social/WeChat'
# 集成QQ
pod ‘UMengUShare/Social/QQ'
end
終端中輸入一下數據
$ cd [存放Podfile的項目路徑]
$ pod update
不可加入--no-repo-update參數,若添加后僅從本地Cocoapods庫中查找SDK,不再更新線上SDK。如果本地存在SDK會直接使用本地SDK版本(不是線上最新版本),若本地不存在SDK會產生錯誤。 也不建議使用pod install命令,這也是從本地SDK庫進行安裝的命令。
3.第三方平臺設置a:配置SSO白名單使用了如SSO授權登錄或跳轉到第三方分享功能,在iOS9/10下就需要增加一個可跳轉的白名單,即LSApplicationQueriesSchemes,否則將在SDK判斷是否跳轉時用到的canOpenURL時返回NO,進而只進行webview授權或授權/分享失敗。 在項目中的info.plist中加入應用白名單,右鍵info.plist選擇source code打開(plist具體設置在Build Setting -> Packaging -> Info.plist File可獲取plist路徑) 請根據選擇的平臺對以下配置進行裁剪:LSApplicationQueriesSchemeswechatweixinsinaweibohdsinaweibosinaweibossoweibosdkweibosdk2.5mqqapimqqmqqOpensdkSSoLoginmqqconnectmqqopensdkdatalinemqqopensdkgrouptribesharemqqopensdkfriendmqqopensdkapimqqopensdkapiV2mqqopensdkapiV3mqqopensdkapiV4mqzoneopensdkwtloginmqqwtloginmqq2mqqwpamqzonemqzonev2mqzonesharewtloginqzonemqzonewxmqzoneopensdkapiV2mqzoneopensdkapi19mqzoneopensdkapimqqbrowsermttbrowserb:URL SchemeURL Scheme是通過系統找到并跳轉對應app的一類設置,通過向項目中的info.plist文件中加入URL types可使用第三方平臺所注冊的appkey信息向系統注冊你的app,當跳轉到第三方應用授權或分享后,可直接跳轉回你的app。添加URL Types可工程設置面板設置
4初始化設置#import在AppDelegate.m中設置如下代碼- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {? ? /* 打開調試日志 */? ? [[UMSocialManager defaultManager] openLog:YES];? ? /* 設置友盟appkey */? ? [[UMSocialManager defaultManager] setUmSocialAppkey:USHARE_DEMO_APPKEY];? ? [self configUSharePlatforms];? ? [self confitUShareSettings];? ? // Custom code? ? return YES;}- (void)confitUShareSettings{? ? /*? ? * 打開圖片水印? ? */? ? //[UMSocialGlobal shareInstance].isUsingWaterMark = YES;? ? /*? ? * 關閉強制驗證https,可允許http圖片分享,但需要在info.plist設置安全域名NSAppTransportSecurityNSAllowsArbitraryLoads*/? ? //[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;}- (void)configUSharePlatforms{? ? /* 設置微信的appKey和appSecret */? ? [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxdc1e388c3822c80b" appSecret:@"3baf1193c85774b3fd9d18447d76cab0" redirectURL:@"http://mobile.umeng.com/social"];? ? /*? ? * 移除相應平臺的分享,如微信收藏? ? */? ? //[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];? ? /* 設置分享到QQ互聯的appID? ? * U-Share SDK為了兼容大部分平臺命名,統一用appKey和appSecret進行參數設置,而QQ平臺僅需將appID作為U-Share的appKey參數傳進即可。? ? */? ? [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1105821097"/*設置QQ平臺的appID*/? appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];? ? /* 設置新浪的appKey和appSecret */? ? [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"3921700954"? appSecret:@"04b48b094faeb16683c32669824ebdad" redirectURL:@"https://sns.whalecloud.com/sina2/callback"]; /* 支付寶的appKey */? ? [[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_AlipaySession appKey:@"2015111700822536" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];}
5.設置系統回調// 支持所有iOS系統- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{? ? BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];? ? if (!result) {? ? ? ? // 其他如支付等SDK的回調? ? }? ? return result;}//支持iOS 9+- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
// 其他如支付等SDK的回調
}
return result;
}
//
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
// 其他如支付等SDK的回調
}
return result;
}
6第三方登錄// 在需要進行獲取登錄信息的UIViewController中加入如下代碼#import- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType 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);
}];
}
7.調用友盟的分享面板#import//顯示分享面板
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
// 根據獲取的platformType確定所選平臺進行下一步操作
}];
8? 第三方平臺登錄授權并獲取用戶信息// 在需要進行獲取登錄信息的UIViewController中加入如下代碼#import- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType{? ? [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType 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);? ? }];}a:新浪微博:授權并獲取用戶信息(獲取uid、access token及用戶名等)// 在需要進行獲取用戶信息的UIViewController中加入如下代碼#import- (void)getAuthWithUserInfoFromSina{? ? [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {? ? ? ? if (error) {? ? ? ? } else {? ? ? ? ? ? UMSocialUserInfoResponse *resp = result;? ? ? ? ? ? // 授權信息? ? ? ? ? ? NSLog(@"Sina uid: %@", resp.uid);? ? ? ? ? ? NSLog(@"Sina accessToken: %@", resp.accessToken);? ? ? ? ? ? NSLog(@"Sina refreshToken: %@", resp.refreshToken);? ? ? ? ? ? NSLog(@"Sina expiration: %@", resp.expiration);? ? ? ? ? ? // 用戶信息? ? ? ? ? ? NSLog(@"Sina name: %@", resp.name);? ? ? ? ? ? NSLog(@"Sina iconurl: %@", resp.iconurl);? ? ? ? ? ? NSLog(@"Sina gender: %@", resp.gender);? ? ? ? ? ? // 第三方平臺SDK源數據? ? ? ? ? ? NSLog(@"Sina originalResponse: %@", resp.originalResponse);? ? ? ? }? ? }];}b:qq:? 授權并獲取用戶信息(獲取uid、access token及用戶名等)- (void)getAuthWithUserInfoFromQQ{? ? [[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);? ? ? ? }? ? }];}c:3? 微信:授權并獲取用戶信息(獲取uid、access token及用戶名等)注意這里的uid為unionID// 在需要進行獲取用戶信息的UIViewController中加入如下代碼#import- (void)getAuthWithUserInfoFromWechat
{
[[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);
}
}];
}