基本组件集成https://developer.umeng.com/docs/66632/detail/66885
U-Share集成https://developer.umeng.com/docs/66632/detail/66825
老版sdk升级文档https://developer.umeng.com/docs/66632/detail/67206#h1-ushare-sdk6
1,Cocoapods集成
网址https://developer.umeng.com/docs/66632/detail/67204#h2--sdk5
快速集成
pod 'UMCCommon'
pod 'UMCCommonLog'
pod 'UMCSecurityPlugins'
# U-Share SDK UI模块(分享面板,建议添加)
pod 'UMCShare/UI'
# 集成微信(精简版0.2M)
pod 'UMCShare/Social/ReducedWeChat'
# 集成微信(完整版14.4M)
pod 'UMCShare/Social/WeChat'
# 集成QQ/QZone/TIM(精简版0.5M)
pod 'UMCShare/Social/ReducedQQ'
# 集成QQ/QZone/TIM(完整版7.6M)
pod 'UMCShare/Social/QQ'
# 集成新浪微博(精简版1M)
pod 'UMCShare/Social/ReducedSina'
# 集成新浪微博(完整版25.3M)
pod 'UMCShare/Social/Sina'
# 集成支付宝
pod 'UMCShare/Social/AlipayShare'
2,配置URL Scheme
目的是能够在分享结束后跳回到自己的app中,Scheme是其他应用跳转回来的标识,而且每个应用的标识还是不同的。具体配置见文档。
根据 appkey配置Schemes,规则如下
来到Info->URL Types配置Schemes
平台 APPKey 举例 注意
QQ 100424468 tencent100424468 QQ需要添加tencent
微信 wxdc1e388c3822c80b wxdc1e388c3822c80b 微信直接用
微博 3921700954 wb3921700954 微博添加wb
3,配置SSO授权白名单
设置白名单为的是能够获取第三方平台的授权.如果你的应用使用了如SSO授权登录或跳转到第三方分享功能,在iOS9/10下就需要增加一个可跳转的白名单,即LSApplicationQueriesSchemes,否则将在SDK判断是否跳转时用到的canOpenURL时返回NO,进而只进行webview授权或授权/分享失败.在项目中的info.plist中加入应用白名单,右键info.plist选择source code打开(plist具体设置在Build Setting -> Packaging -> Info.plist File可获取plist路径)
快速集成plist配置(包括相册权限,soo白名单,Scheme配置,包含微博,QQ,微信,支付宝,需要其他的可以自己参考文档配置)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>wxdc1e388c3822c80b</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>tencent100424468</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>QQ05fc5b14</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>wb3921700954</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>ap2015111700822536</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<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>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>
<string>tim</string>
<string>timapi</string>
<string>timopensdkfriend</string>
<string>timwpa</string>
<string>timgamebindinggroup</string>
<string>timapiwallet</string>
<string>timOpensdkSSoLogin</string>
<string>wtlogintim</string>
<string>timopensdkgrouptribeshare</string>
<string>timopensdkapiV4</string>
<string>timgamebindinggroup</string>
<string>timopensdkdataline</string>
<string>wtlogintimV1</string>
<string>timapiV1</string>
<string>alipay</string>
<string>alipayshare</string>
</array>
4,代码部分
4.1 pch导入所需要的头文件
#import <UMShare/UMShare.h>
#import <UShareUI/UShareUI.h>
#import <UMCommon/UMCommon.h>
4.2,初始化友盟
这里的Your appkey填在友盟注册的appkey
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// U-Share 平台设置
[UMConfigure setEncryptEnabled:YES];//打开加密传输
[UMConfigure setLogEnabled:YES];//设置打开日志,发布必须设置为NO
[UMConfigure initWithAppkey:@"5b63ed31b27b0a1d82000286" channel:@"App Store"];
NSString* deviceID = [UMConfigure deviceIDForIntegration];
if ([deviceID isKindOfClass:[NSString class]]) {
NSLog(@"服务器端成功返回deviceID = %@",deviceID);
}
else{
NSLog(@"服务器端还没有返回deviceID");
}
[self configUSharePlatforms];
[self confitUShareSettings];
return YES;
}
4.3显示分享面板
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//显示分享面板
__weak typeof(self) weakSelf = self;
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
[weakSelf shareToPlatformType:platformType];
}];
}
4.4,设置分享面板内容(注册各个平台的 appkey)
这个 key 是到响应的平台创建的,用来标识给什么应用授权
- (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"];
}
4.5,判断应用是否安装
#pragma mark 判断是否安装分享平台
- (BOOL)isInstall:(UMSocialPlatformType)platformType {
if (![[UMSocialManager defaultManager] isInstall:platformType]) {
if (platformType == UMSocialPlatformType_QQ) {
//qq
NSLog(@"未安装QQ");
} else {
//weixin
}
return NO;
}
return YES;
}
4.6,分享到指定平台
- (void)shareToPlatformType:(UMSocialPlatformType)platformType {
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建分享的网页
/**
* @param title 标题
* @param descr 描述
* @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
*
*/
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"分享到标题" descr:@"分享的描述" thumImage:[UIImage imageNamed:@"3.png"]];
shareObject.webpageUrl = @"https://www.baidu.com/";
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//判断是否安装所分享到的平台, 没安装则不分享,给出提示。
if (![self isInstall:platformType]) {
return ;
}
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
;
//分享失败
if (error) {
}else{
if (platformType == UMSocialPlatformType_WechatTimeLine) {
//分享到朋友圈成功
} else {
//分享到其他平台成功
}
}
}];
}
5,三方授权
微博授权要求绑定 builde id
- (IBAction)qqLogin:(id)sender {
/**
设置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);
}
}];
}
- (IBAction)weChatLogin:(id)sender {
[[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);
}
}];
}