美文网首页
UIActivityViewController分享视频到第三方

UIActivityViewController分享视频到第三方

作者: 路漫漫其修远兮Wzt | 来源:发表于2018-12-25 17:25 被阅读20次

    一、分享视频到App,AirPort、邮件等

        NSString *localPath = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"MP4"];
        NSURL *urlPath1 = [NSURL fileURLWithPath:localPath];
        NSURL *urlPath2 = [NSURL URLWithString:localPath];
    //urlPath1 = @"file:///var/containers/Bundle/Application/56D4C879-4242-44ED-877A-255C9BAD87AA/ActivityAirPortDemo.app/test.MP4";
    //urlPath2 = @"/var/containers/Bundle/Application/56D4C879-4242-44ED-877A-255C9BAD87AA/ActivityAirPortDemo.app/test.MP4";
    

    1.items

        //需要分享的内容: 标题、图片、url
        NSMutableArray *items = [[NSMutableArray alloc] init];
        if (title) {
            [items addObject:title];
        }
        if (image) {
            [items addObject:image];
        }
        NSString *localPath = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"MP4"];
        if (localPath) {
            //*** 这里URL是用[NSURL fileURLWithPath:localPath]; ***
            NSURL *urlPath = [NSURL fileURLWithPath:localPath];
            [items addObject:urlPath];
        }
    

    2.activities

        //自定义“分享目标icon”
        NSMutableArray *activities = [[NSMutableArray alloc] init];
        //youtube
        HBShareBaseActivity *youtubeActivity = [[HBShareBaseActivity alloc] initWithTitle:@"YouTube" type:kYouTubeType path:_localPath];
        //mail
        HBShareBaseActivity *mailActivity = [[HBShareBaseActivity alloc] initWithTitle:NSLocalizedString(@"bc.player.share_mail_title", 0) type:UIActivityTypeMail path:_localPath];//
        //
        [@[mailActivity, youtubeActivity] enumerateObjectsUsingBlock:^(HBShareBaseActivity *activity, NSUInteger idx, BOOL *stop) {
            activity.shareDescription = description;
            activity.shareTitle = title;
            activity.shareImage = image;
        }];
        [activities addObjectsFromArray:@[mailActivity, youtubeActivity]]; //
        
        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:activities];
    
        //需要屏蔽的系统分享
        NSArray *excludedActivityTypes =  @[UIActivityTypePostToFacebook,
                                            UIActivityTypePostToTwitter,
                                            UIActivityTypePostToWeibo,
                                            UIActivityTypeMessage,
                                            UIActivityTypeMail,
                                            UIActivityTypePrint,
                                            UIActivityTypeCopyToPasteboard,
                                            UIActivityTypeAssignToContact,
                                            UIActivityTypeSaveToCameraRoll,
                                            UIActivityTypeAddToReadingList,
                                            UIActivityTypePostToFlickr,
                                            UIActivityTypePostToVimeo,
                                            UIActivityTypePostToTencentWeibo,
                                            //UIActivityTypeAirDrop,
                                            UIActivityTypeOpenInIBooks,
                                            //UIActivityTypeMarkupAsPDF
                                            ];
        
        activityViewController.excludedActivityTypes = excludedActivityTypes;
        AppDelegate *iApp = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [iApp.window.rootViewController presentViewController:activityViewController animated:YES completion:nil];
        //[UIViewController.topVC presentViewController:activityViewController animated:YES completion:nil];
        
        activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
            NSLog(@"social share activityType=%@, returnedItems=%@", activityType, returnedItems);
            if (completionHandler) {
                completionHandler(activityType, completed);
                self.completionHandler = nil;
            }
        };
    

    二、MFMailComposeViewController发送邮件

    - (void)sendMailAction {
        
        //附件
        NSData* pData = [[NSData alloc]initWithContentsOfFile:self.urlPath];
        if (pData) {
            
            MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
            _mailCompose = mailCompose;
            if(mailCompose) {
                
                //设置代理
                [mailCompose setMailComposeDelegate:self];
                
                NSArray *toAddress = [NSArray arrayWithObject:@""]; //收件人
                NSArray *ccAddress = [NSArray arrayWithObject:@""]; //抄送人
                NSString *emailBody = @"<H1></H1>";                 //邮件内容
                
                //设置收件人
                [mailCompose setToRecipients:toAddress];
                //设置抄送人
                [mailCompose setCcRecipients:ccAddress];
                //设置邮件内容
                [mailCompose setMessageBody:emailBody isHTML:YES];
                
                //设置邮件主题
                [mailCompose setSubject:@""];           //邮件主题 email title
                //设置邮件附件{mimeType:文件格式|fileName:文件名}
                NSString *lastName = [_urlPath componentsSeparatedByString:@"/"].lastObject;
                NSString *fileExtension = [lastName componentsSeparatedByString:@"."].lastObject;
                if (!lastName) {
                    lastName = @"test.mp4";
                }
                if (!fileExtension) {
                    fileExtension = @"mp4";
                }
                [mailCompose addAttachmentData:pData mimeType:fileExtension fileName:lastName];
                //设置邮件视图在当前视图上显示方式
                AppDelegate *iApp = (AppDelegate*)[UIApplication sharedApplication].delegate;
                [iApp.window.rootViewController presentViewController:mailCompose animated:YES completion:nil];
                //[UIViewController.topVC presentViewController:mailCompose animated:YES completion:nil];
            }
            return;
        }
    }
    
    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
        
        NSString *msg = nil;
        switch (result) {
            case MFMailComposeResultCancelled:
                msg = @"邮件发送取消";
                break;
            case MFMailComposeResultSaved:
                msg = @"邮件保存成功";
                break;
            case MFMailComposeResultSent:
                msg = @"邮件发送成功";
                break;
            case MFMailComposeResultFailed:
                msg = @"邮件发送失败";
                break;
            default:
                break;
        }
        NSLog(@"发送邮件: %@", msg);
        [_mailCompose dismissViewControllerAnimated:YES completion:nil];
    }
    

    demo地址

    相关文章

      网友评论

          本文标题:UIActivityViewController分享视频到第三方

          本文链接:https://www.haomeiwen.com/subject/pbaflqtx.html