背景:
友盟分享的文檔太坑了,這里自己總結(jié)一下说墨,只需5步。
1苍柏、申請得到微信對應(yīng)的appid和appSecrect尼斧,在友盟官網(wǎng)中創(chuàng)建app,得到友盟的對應(yīng)的app 的key试吁;
2棺棵、pod導(dǎo)入分享的第三方平臺:
# 友盟基礎(chǔ)
pod 'UMCCommon'
# 集成微信(精簡版0.2M)
pod 'UMCShare/Social/ReducedWeChat'
3、在info.plist中添加
(1)對應(yīng)的第三方app 的白名單潘悼;
(2)設(shè)置URL Type律秃,跳轉(zhuǎn)到第三方app需要添加
image.png
4、創(chuàng)建一個appdelegate的分類治唤,在appdelegate的didFinishLaunchingWithOptions中注冊友盟的key棒动,和對應(yīng)第三方app的id和secret;
/**
* 注冊友盟分享
*/
-(void)registerUmeng{
[UMConfigure initWithAppkey:UMengShare_KEY channel:@"App Store"];
// 第三方分享平臺
[self configUSharePlatforms];
}
/**
* 友盟分享第三方appid和appSecret設(shè)置
*/
-(void)configUSharePlatforms{
/* 設(shè)置微信的appKey和appSecret */
// 微信聊天
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WeChatShare_APPID appSecret:WeChatShare_Secret redirectURL:@"http://mobile.umeng.com/social"];
// 微信朋友圈
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatTimeLine appKey:WeChatShare_APPID appSecret:WeChatShare_Secret redirectURL:@"http://mobile.umeng.com/social"];
/*
* 移除相應(yīng)平臺的分享宾添,如微信收藏
*/
//[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
/* 設(shè)置分享到QQ互聯(lián)的appID
* U-Share SDK為了兼容大部分平臺命名船惨,統(tǒng)一用appKey和appSecret進行參數(shù)設(shè)置,而QQ平臺僅需將appID作為U-Share的appKey參數(shù)傳進即可缕陕。
*/
/* QQ
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"“ appSecret:@”“ redirectURL:@"http://mobile.umeng.com/social"];
*/
/* 新浪微博
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"" appSecret:@"" redirectURL:@"https://sns.whalecloud.com/sina2/callback"];
*/
}
5粱锐、點擊第三方app的圖標,分享網(wǎng)頁(注:建議所有的第三方庫都自己在封裝一層扛邑,不僅在多個頁面中使用的時候方便怜浅,在新的app中也方便代碼copy);
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
//創(chuàng)建分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//創(chuàng)建網(wǎng)頁內(nèi)容對象
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"分享標題" descr:@"分享內(nèi)容描述" thumImage:[UIImage imageNamed:@"icon"]];
//設(shè)置網(wǎng)頁地址
shareObject.webpageUrl =@"http://mobile.umeng.com/social";
//分享消息對象設(shè)置分享內(nèi)容對象
messageObject.shareObject = shareObject;
//調(diào)用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}