UIActivityViewController分享視訊到第三方App

weixin_34019929發表於2018-12-25

一、分享視訊到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地址

相關文章