iOS利用系统分享实现分享视频功能

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];
    
    //给activityVC的属性completionHandler写一个block。
    //用以UIActivityViewController执行结束后,被调用,做一些后续处理。
    UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType activityType, BOOL completed, NSArray * returnedItems, NSError * activityError)
    {
        LyLog(@"activityType :%@", activityType);
        if (completed)
        {
            LyLog(@"completed");
        }
        else
        {
            LyLog(@"cancel");
        }
    };
    
    // 初始化completionHandler,当post结束之后(无论是done还是cancell)该blog都会被调用
    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.另外需要注意的地方

当弹出分享列表,但是点击第三方平台按钮没有响应是,去设置里面看下



点开添加账号信息



已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页