Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video not attached for mail when sharing video using UIActivityViewController

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.

like image 368
gabbler Avatar asked Jan 29 '15 07:01

gabbler


1 Answers

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

                });


        }
    }];
}];
like image 110
gabbler Avatar answered Oct 23 '22 23:10

gabbler