***添加友盟SDK和静态库***
解压iOS SDK压缩包,将其中的UMSocial_Sdk_4.0和UMSocial_Sdk_Extra_Frameworks文件夹加入到工程中去,再添加系统framework:
Security.framework,libiconv.dylib,SystemConfiguration.framework,CoreGraphics.framework,libsqlite3.dylib,CoreTelephony.framework,libstdc++.dylib,libz.dylib。
友盟分享使用:
1.基本功能集成:
在AppDelegate内设置友盟AppKey
#import "UMSocial.h"
……
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UMSocialData setAppKey:@"507fcab25270157b37000010"];
//打开日志调用
[UMSocialDataopenLog:YES];
}
在ViewController.m中添加实现基本分享功能的代码,各个参数的设置请参考绿字注释部分:
[UMSocialSnsService presentSnsIconSheetView:self
appKey:@"507fcab25270157b37000010"
shareText:@"你要分享的文字"
shareImage:[UIImage imageNamed:@"icon.png"]
shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent, UMShareToRenren,nil]
delegate:nil];
2.使用直接分享底层接口
13 | [[UMSocialDataServicedefaultDataService] postSNSWithTypes:@[UMShareToSina]content:@ "今天又没有吃药!" image:[UIImage imageNamed:@ "001.gif" ] location:nilurlResource:nilpresentedController:self completion:^(UMSocialResponseEntity *response) { |
14 | if (response.responseCode == UMSResponseCodeSuccess) { |
15 | NSLog(@ "respon%@" ,response); |
3.分享url资源
4 | [[UMSocialDataServicedefaultDataService]postSNSWithTypes:@[UMShareToSina]content:@ "Chinajoy的萌妹子" image:nillocation:nilurlResource:urlResource presentedController:selfcompletion:^(UMSocialResponseEntity *response) { |
5 | if (response.responseCode == UMSResponseCodeSuccess) { |
效果如下:
4.分享本地动态gif图片
把本地的gif图片声称NSData对象,再进行分享,参考如下方法:
1 | NSString *path = [[NSBundlemainBundle]pathForResource:@ "001" ofType:@ "gif" ]; |
2 | NSData *gifData = [NSDatadataWithContentsOfFile:path]; |
3 | [UMSocialSnsServicepresentSnsIconSheetView:selfappKey:APPKEYshareText:@ "今天一直都要萌萌哒!" shareImage:gifData shareToSnsNames:nildelegate:self]; |
5.摇一摇截屏分享
实现摇一摇截图分享需要导入两个头文件,功能实现如下:
01 | #import "UMSocialShakeService.h" |
02 | #import "UMSocialScreenShoter.h" |
12 | [UMSocialShakeServicesetShakeToShareWithTypes:@[UMShareToTencent,UMShareToSina,UMShareToRenren]shareText:@ "摇一摇" screenShoter:[UMSocialScreenShoterDefaultscreenShoter] inViewController:selfdelegate:nil]; |
14 | [UMSocialShakeServicesetShakeThreshold:1.0]; |
手机端摇一摇出现如下界面:

6.进入授权信息页面
个人中心的登录账号可以选择下面已经授权的平台登录
1 | UINavigationController *accountViewController = [[UMSocialControllerServiceCommentdefaultControllerService] getSocialAccountController]; |
2 | [selfpresentModalViewController:accountViewController animated:YES]; |
效果图如下:

7.获取授权用户信息
02 | BOOL isOauth = [UMSocialAccountManagerisOauthAndTokenNotExpired:UMShareToSina]; |
05 | NSDictionary *snsAccountDic = [UMSocialAccountManagersocialAccountDictionary]; |
07 | UMSocialAccountEntity *sinaAccount = [snsAccountDic valueForKey:UMShareToSina]; |
08 | NSLog(@ "sina name is %@, icon URL is %@" ,sinaAccount.userName,sinaAccount.iconURL); |
10 | [[UMSocialDataServicedefaultDataService]requestSnsInformation:UMShareToSinacompletion:^(UMSocialResponseEntity *response) { |
11 | NSLog(@ "response is %@" ,response.data); |
12 | self.textView.text = [response.datadescription]; |
16 | [UMSocialSnsPlatformManagergetSocialPlatformWithName:UMShareToSina].loginClickHandler(self,[UMSocialControllerServicedefaultControllerService],YES,^(UMSocialResponseEntity *response){ |
17 | if (response.responseCode == UMSResponseCodeSuccess) { |
19 | UMSocialAccountEntity *snsAccount = [[UMSocialAccountManagersocialAccountDictionary] valueForKey:UMShareToSina]; |
20 | NSLog(@ "username is %@, uid is %@, token is %@" ,snsAccount.userName,snsAccount.usid,snsAccount.accessToken); }});} |
8.社会化操作栏
2 | UMSocialData *socialData =[[UMSocialDataalloc]initWithIdentifier:@ "A Book Named BILI" ]; |
3 | UMSocialBar *socialBar = [[UMSocialBaralloc]initWithUMSocialData:socialData withViewController:self]; |
4 | socialBar.center = CGPointMake(160, 435); |
5 | [self.viewaddSubview:socialBar]; |
在屏幕的下方会出现这样的一个操作栏,从左往右依次是评论、喜欢、分享、用户信息:

DEMO展示
01 | - (IBAction)share:(id)sender { |
07 | [UMSocialSnsServicepresentSnsIconSheetView:selfappKey:APPKEYshareText:@ "好开心啊,今天又没有吃药!" shareImage:[UIImage imageNamed:@ "001.gif" ] shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToQzone,UMShareToSina,UMShareToTencent,UMShareToRenren,nil] delegate:nil]; |
10 | - (IBAction)postShare:(id)sender { |
12 | [[UMSocialDataServicedefaultDataService] postSNSWithTypes:@[UMShareToSina]content:@ "今天又没有吃药!" image:[UIImage imageNamed:@ "001.gif" ] location:nilurlResource:nilpresentedController:self completion:^(UMSocialResponseEntity *response) { |
13 | if (response.responseCode == UMSResponseCodeSuccess) { |
14 | NSLog(@ "respon%@" ,response); |
21 | - (IBAction)shareUrlResource:(id)sender { |
29 | [[UMSocialDataServicedefaultDataService]postSNSWithTypes:@[UMShareToSina]content:@ "Chinajoy的萌妹子" image:nillocation:nilurlResource:urlResource presentedController:selfcompletion:^(UMSocialResponseEntity *response) { |
30 | if (response.responseCode == UMSResponseCodeSuccess) { |
36 | - (IBAction)shareGif:(id)sender { |
37 | NSString *path = [[NSBundlemainBundle]pathForResource:@ "001" ofType:@ "gif" ]; |
38 | NSData *gifData = [NSDatadataWithContentsOfFile:path]; |
39 | [UMSocialSnsServicepresentSnsIconSheetView:selfappKey:APPKEYshareText:@ "今天一直都要萌萌哒!" shareImage:gifData shareToSnsNames:nildelegate:self]; |
42 | - (IBAction)shakeAndShare:(id)sender { |
43 | [UMSocialShakeServicesetShakeToShareWithTypes:nil |
50 | - (IBAction)shakeScreenShoot:(id)sender { |
60 | [UMSocialShakeServicesetShakeToShareWithTypes:@[UMShareToTencent,UMShareToSina,UMShareToRenren]shareText:@ "摇一摇" screenShoter:[UMSocialScreenShoterDefaultscreenShoter] inViewController:selfdelegate:nil]; |
62 | [UMSocialShakeServicesetShakeThreshold:1.0]; |
64 | - (IBAction)login:(id)sender { |
65 | UINavigationController *accountViewController = [[UMSocialControllerServiceCommentdefaultControllerService] getSocialAccountController]; |
66 | [selfpresentModalViewController:accountViewController animated:YES]; |
遇到问题
在分享url资源的时候,笔者使用了setResourceType这个方法来设置url图片为分享内容并且发送了一个分享,分享以后发现其它分享方式的图片都变成了这个url图片,如果只是需要这一个分享使用的,而不改变其它分享方式的图片,则需要创建一个UMSocialUrlResource对象,用postSNSWithTypes方法发送分享。详情可以参考demo中的shareUrlResource方法。
开发文档
友盟社会化组件API列表下载地址:
http://dev.umeng.com/system/resources/W1siZiIsIjIwMTQvMDcvMDIvMTZfMTJfNTJfOTk3X1VNU29jaWFsX1Nka19BcGlfUmV
mZXJlbmNlLnppcCJdXQ/UMSocial_Sdk_Api_Reference.zip
iOS SDK下载时可选一个Demo测试源码工程,可以参考这个工程和API文档完成项目集成。
特此声明:此内容整理于DevStore评测内容
感谢DevStore提供的内容供广大开发者参考