在iOS实际开发中,经常会用到iOS分享到第三方的平台,为了方便开发,苹果继承了一些分享平台功能。
1、创建工程的时候,要先带入:Social.framework系统包
2、以下是代码实现:
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200, 44);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)buttonClick{
// 设置分享内容
NSString *text = @"分享内容";
//UIImage *image = [UIImage imageNamed:@"1.png"];
NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/us/app/id1141820521"];
NSArray *activityItems = @[text,url];
// 服务类型控制器
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.modalInPopover = true;
[self presentViewController:activityViewController animated:YES completion:nil];
// 选中分享类型
[activityViewController setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
// 显示选中的分享类型
NSLog(@"act type %@",activityType);
if (completed) {
NSLog(@"ok");
}else {
NSLog(@"no ok");
}
}];
3、分享的时候如下图:
要先加载更多,然后给开启分享的权限。如果是微博分享,要现在手机设置里面登录邮箱,不然无法登录。
github下载地址:https://github.com/fenglingdeyi/NativeShare
更多参考地址:http://blog.csdn.net/qq_32510689/article/details/51781167