5

I am using the below code to share a video located on device, it works great for sharing via message, facebook and iCloud, only not for mail, I can see the mail option is there, but in the mail draft, the video is not attached.

In the code, videoAsset is a PHAsset of type PHAssetMediaTypeVideo.

[[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
    AVURLAsset* urlAsset = (AVURLAsset*)asset;
    fileUrl = urlAsset.URL;
    NSLog(@"fileUrl is %@",fileUrl);

    NSArray *activityItems = [NSArray arrayWithObjects:fileUrl, nil];

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityViewController animated:YES completion:nil];
}];

If I attach a video using UIImagePickerController, it works, I searched but couldn't find an answer, please help.

gabbler
  • 13,626
  • 4
  • 32
  • 44
  • http://stackoverflow.com/a/20211603/2074320 – Mitul Marsoniya Jan 31 '15 at 05:19
  • @mitulmarsonia what? – gabbler Jan 31 '15 at 05:30
  • The options shown in the UIActivityViewController totally depends on the type of items that are to be shared. For example, if there is a video, it will not show Facebook or twitter option. But if it's an image and title, it definitely will show the relevant options. The following will show up apps like mail, twitter, Facebook, assignToContact, save to camera roll, print, copy, etc – Mitul Marsoniya Jan 31 '15 at 05:35
  • @mitulmarsonia, **if there is a video, it will not show Facebook or twitter option**, that is not true, for me Facebook is shown, and I can share the video to facebook, the problem is when I share a video, mail option is shown, but the video isn't attached when I try to send the mail, nor do the receiver receive the video. – gabbler Jan 31 '15 at 06:00

1 Answers1

12

I ended up saving the video file to document directory, and using the file url from document directory, the video is attached for sharing via mail.

[[PHImageManager defaultManager] requestImageDataForAsset:videoAsset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
    NSLog(@"info is %@", info);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* videoPath = [documentsDirectory stringByAppendingPathComponent:@"IMG_2185.MOV"];

    NSError *error;
    [[NSFileManager defaultManager] removeItemAtPath:videoPath error:&error];
    BOOL success = [imageData writeToFile:videoPath atomically:YES];
    if (success) {
        NSArray *activityItems = [NSArray arrayWithObjects:[NSURL fileURLWithPath:videoPath], nil];

        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
        [self presentViewController:activityViewController animated:YES completion:nil];
    }
}];

I also used requestExportSessionForVideo method to export a video to document directory, which also worked.

[[PHImageManager defaultManager] requestExportSessionForVideo:videoAsset options:nil exportPreset:AVAssetExportPresetPassthrough resultHandler:^(AVAssetExportSession *exportSession, NSDictionary *info) {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* videoPath = [documentsDirectory stringByAppendingPathComponent:@"test3.MOV"];
    NSFileManager *manager = [NSFileManager defaultManager];

    NSError *error;
    if ([manager fileExistsAtPath:videoPath]) {
        BOOL success = [manager removeItemAtPath:videoPath error:&error];
        if (success) {
            NSLog(@"I successfully removed it!");
        }
    }

    NSURL *outputURL = [NSURL fileURLWithPath:videoPath];
    NSLog(@"this is the final path %@",outputURL);
    exportSession.outputFileType=AVFileTypeQuickTimeMovie;
    exportSession.outputURL=outputURL;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        if (exportSession.status == AVAssetExportSessionStatusFailed) {
            NSLog(@"failed");
        } else if(exportSession.status == AVAssetExportSessionStatusCompleted){
            NSLog(@"completed!");
                dispatch_async(dispatch_get_main_queue(), ^(void) {
                    NSArray *activityItems = [NSArray arrayWithObjects:outputURL, nil];

                    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
                    [self presentViewController:activityViewController animated:YES completion:nil];
                    //Main

                });


        }
    }];
}];
ninjaneer
  • 6,951
  • 8
  • 60
  • 104
gabbler
  • 13,626
  • 4
  • 32
  • 44
  • Do you happen to have a snippet of how you used `requestExportSessionForVideo`? I'm trying to use it but the outputURL always comes as nil. Thanks! – batkru Jan 31 '15 at 04:57
  • Amaaaaaaaaaaazing! I had spent so much time trying without luck! Thank you very very much – batkru Jan 31 '15 at 05:34
  • How to share several video? Using for loop and exporting all video? – Hwangho Kim Apr 20 '16 at 10:54
  • @HwanghoKim, haven't tried it, but `activityItems` is an array, you can try add multiple videos to it. – gabbler Apr 20 '16 at 12:02
  • 1
    So after shared, is the video deleted from Documents directory? How's that handled? – Gizmodo May 01 '17 at 16:30