1. 先说说自己走的弯路
要使用系统分享,由于之前没接触过,上网发现UIActivityViewController可以实现原生分享。UIActivityViewController可以分享文本、图片、链接,
- NSString *textToShare = @"文本内容";
- UIImage *imageToShare = [UIImage imageNamed:@"imagename"];
- NSURL *urlToShare = [NSURL URLWithString:@"http:baidu.com"];
- NSArray *activityItems = @[textToShare, imageToShare, urlToShare];
-
- UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems
- applicationActivities:nil];
-
- activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList];
-
-
-
- UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType activityType, BOOL completed, NSArray * returnedItems, NSError * activityError)
- {
- LyLog(@"activityType :%@", activityType);
- if (completed)
- {
- LyLog(@"completed");
- }
- else
- {
- LyLog(@"cancel");
- }
- };
-
-
- activityVC.completionWithItemsHandler = myBlock;
-
- UIViewController * rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
- [rootVc presentViewController:activityVC animated:TRUE completion:nil];
具体使用方法可参考:http://www.jianshu.com/p/a1c9621a3f4e
本来以为UIActivityViewController 不能分享视频,经网友cys482提醒,说构建NSURL时采用 [NSURL fileURLWithPath:urlString];就可以分享视频
然后我试了下
- NSString *textToShare = @"文本内容";
- UIImage *imageToShare = [UIImage imageNamed:@"test"];
- NSURL*urlToShare= [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]];
- NSArray *activityItems = @[textToShare,imageToShare,urlToShare];
然后出现
然后修改activityItems 只包含url
- NSString *textToShare = @"文本内容";
- UIImage *imageToShare = [UIImage imageNamed:@"test"];
- NSURL*urlToShare= [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]];
- NSArray *activityItems = @[urlToShare];
bing~go ,分享成功!!!
2. UIDocumentInteractionController 具有预览和分享的功能
- NSURL * url = [NSURL fileURLWithPath:filePath];
- _documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
- UIViewController * vc = [UIApplication sharedApplication].keyWindow.rootViewController;
- [self.documentController presentOpenInMenuFromRect:[UIScreen mainScreen].bounds inView:vc.view animated:YES];
- self.documentController.delegate = self;
其中filePath是视频文件地址
UIDocumentInteractionController的用法具体可参考:http://www.jianshu.com/p/3f03897cf98a
3.另外需要注意的地方
当弹出分享列表,但是点击第三方平台按钮没有响应是,去设置里面看下
![](https://img-blog.csdn.net/20171217182338079?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHhsenk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
点开添加账号信息
![](https://img-blog.csdn.net/20171217182343912?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHhsenk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
转载自:http://blog.csdn.net/liyun123gx/article/details/60875633