我在網(wǎng)上看了很多關(guān)于友盟分享的文章,可是都覺得不是很清楚,所以就結(jié)合友盟的官方文檔自己研究了一下。
首先,因為懶所以我先試了下文檔說的通過Cocoapods集成,但是沒有成功,可能要先把下載好的SDK放在跟工程同一級目錄然后再pod 'UMengUShare/UI'才有效,但我懶所以這個沒有嘗試,然后我有試了下pod 'UMengSocialCOM', '~> 5.2.1',這個可以了,但是pod 回來的包有100多M,太大了,因為它里面包含了各個平臺的分享,然而我工程只需要用到微信、qq和微博,所以后來用了手動引入,下面說一下。
手動引入的第一步,上友盟根據(jù)自己的需要把SDK下載回來,需要多少個分享平臺就選多少個平臺,多選一點的話SDK也會大一點,下載回來了把不需要的拖出工程也可以。





下載完回來會把UMSocial這個文件夾拖進工程里面

再配置需要的環(huán)境(按照文檔來就好了)
1、在Other Linker Flags加入-ObjC

2、加入依賴系統(tǒng)庫


3、配置各平臺URL Scheme(首先要去各平臺申請一個appKey)
這一步是在第三方應(yīng)用授權(quán)或分享后,跳轉(zhuǎn)回你的app用的,沒有配置的話就跳不回來。
有3種方式設(shè)置
①通過工程設(shè)置面板

②通過info.plist文件編輯

③直接編輯info.plist中XML代碼

最后就可以敲代碼了
在AppDelegate.m中設(shè)置如下代碼
引入頭文件#import <UMSocialCore/UMSocialCore.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//打開調(diào)試日志
[[UMSocialManager defaultManager] openLog:YES];
//設(shè)置友盟appkey
[[UMSocialManager defaultManager] setUmSocialAppkey:@"57b432afe0f55a9832001a0a"];
// 獲取友盟social版本號
//NSLog(@"UMeng social version: %@", [UMSocialGlobal umSocialSDKVersion]);
//設(shè)置微信的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxdc1e388c3822c80b" appSecret:@"3baf1193c85774b3fd9d18447d76cab0" redirectURL:@"http://mobile.umeng.com/social"];
//設(shè)置分享到QQ互聯(lián)的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"100424468"? appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
//設(shè)置新浪的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"3921700954"? appSecret:@"04b48b094faeb16683c32669824ebdad" redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
//支付寶的appKey
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_AlipaySession appKey:@"2015111700822536" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
//設(shè)置易信的appKey
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_YixinSession appKey:@"yx35664bdff4db42c2b7be1e29390c1a06" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
//設(shè)置點點蟲(原來往)的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_LaiWangSession appKey:@"8112117817424282305" appSecret:@"9996ed5039e641658de7b83345fee6c9" redirectURL:@"http://mobile.umeng.com/social"];
//設(shè)置領(lǐng)英的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Linkedin appKey:@"81t5eiem37d2sc"? appSecret:@"7dgUXPLH8kA8WHMV" redirectURL:@"https://api.linkedin.com/v1/people"];
//設(shè)置Twitter的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Twitter appKey:@"fB5tvRpna1CKK97xZUslbxiet"? appSecret:@"YcbSvseLIwZ4hZg9YmgJPP5uWzd4zr6BpBKGZhf07zzh3oj62K" redirectURL:nil];
// 如果不想顯示平臺下的某些類型,可用以下接口設(shè)置
//? ? [[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite),@(UMSocialPlatformType_YixinTimeLine),@(UMSocialPlatformType_LaiWangTimeLine),@(UMSocialPlatformType_Qzone)]];
...
return YES;
}
設(shè)置系統(tǒng)回調(diào)
使用
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
// 其他如支付等SDK的回調(diào)
}
return result;
}
或
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
// 其他如支付等SDK的回調(diào)
}
return result;
}
第三方平臺授權(quán)
支持授權(quán)的平臺
微信、QQ、新浪微博、騰訊微博、人人網(wǎng)、豆瓣、Facebook、Twitter、Linkedin領(lǐng)英、Kakao
授權(quán)并獲取用戶信息
// 在需要進行獲取用戶信息的UIViewController中加入如下代碼
#import
- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
UMSocialUserInfoResponse *userinfo =result;
NSString *message = [NSString stringWithFormat:@"name: %@\n icon: %@\n gender: %@\n",userinfo.name,userinfo.iconurl,userinfo.gender];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UserInfo"
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"確定", nil)
otherButtonTitles:nil];
[alert show];
}];
}
最后就是在你需要分享的控制器里面加上下面幾句代碼
第三方平臺分享
彈出分享面板(可以自定義)
#import "UMSocialUIManager.h"
//點擊分享按鈕
- (IBAction)share:(id)sender {
__weak typeof(self) weakSelf = self;
//顯示分享面板
[UMSocialUIManager showShareMenuViewInView:nil sharePlatformSelectionBlock:^(UMSocialShareSelectionView *shareSelectionView, NSIndexPath *indexPath, UMSocialPlatformType platformType) {
[weakSelf disMissShareMenuView];
[weakSelf shareDataWithPlatform:platformType];
}];
}
設(shè)置分享內(nèi)容(這里才是真正點擊了分享要調(diào)用的方法)
分享文本
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType
{
//創(chuàng)建分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//設(shè)置文本
messageObject.text = @"社會化組件UShare將各大社交平臺接入您的應(yīng)用,快速武裝App。";
//調(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);
}
}];
}
另外,如果要做iOS9/10系統(tǒng)的適配的話
http://dev.umeng.com/social/ios/ios9
按照這個說的去做就可以了,就是把里面的XML格式的代碼添加到你的info.plist文件里面就好