Lam
Lam

Reputation: 23

iOS Could not share video to Whatsapp using UIDocumentInteractionController

It successfully redirects to the Whatsapp app.There is no preview frame and when tapped Send, an error messages pops up "This video could not be sent. Please choose a different video.

Here is my code.

- (void)shareVideo {
NSLog(@"[WhatsAppShare] sharing video");
//NSString *nativePath = [[NSString alloc] initWithCString:path encoding:NSASCIIStringEncoding];
NSString *nativePath=[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];

// Save video to path in documents directory
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wam"];

if([[NSFileManager defaultManager] fileExistsAtPath:savePath]){
    if([[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]){
        [self shareVideoAtNativePath:nativePath SavePath:savePath];
    }
} else {
    [self shareVideoAtNativePath:nativePath SavePath:savePath];
}}

- (void)shareVideoAtNativePath:(NSString*)nativePath SavePath:(NSString*)savePath{
NSError*error;
BOOL isSuccess=[[NSFileManager defaultManager] copyItemAtPath:nativePath toPath:savePath error:&error];
if(isSuccess){
    // Create interaction controller
    self.documentInteractionController          = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
    self.documentInteractionController.UTI      = @"net.whatsapp.movie";
    self.documentInteractionController.delegate = self;

    [self.documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 1, 1)
                                                           inView:[self view]
                                                         animated:YES];
} else{
    NSLog(@"error %@", error);
}
}

Upvotes: 2

Views: 2143

Answers (3)

Ramani Hitesh
Ramani Hitesh

Reputation: 214

// In code Use share GIF and Video for WhatsApp....

  NSString    *savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wam"];
savePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:_videourl];
_documentInteractionController.UTI = @"net.whatsapp.movie";
_documentInteractionController.delegate = (id)self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];

Upvotes: -1

Prasad Pawar
Prasad Pawar

Reputation: 1701

We've faced the exact same issue.

The official .wam format brings up only WhatsApp in the share dialog, but fails to forward the video.

Using .m4v format is working for us. A few more options are displayed along with WhatsApp (Open in WhatsApp is the option we want). We are displaying an alert saying "Please select Whatsapp on the next screen" before sending user to the share dialog.

File format: m4v

UTI: net.whatsapp.movie

Please refer to the working code below:

 UIDocumentInteractionController *documentInteractionController;
    -----
    -----
 - (void)shareVideoViaWhatsApp:(NSURL*)url{    
        // Creating temp video to share specifically on whatsapp.   
        NSString *cachesFolder = [NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"video.m4v"]];
        NSURL *file = [NSURL fileURLWithPath:cachesFolder];
        [[NSData dataWithContentsOfURL:url] writeToURL:file options:NSDataWritingAtomic error:nil];

        documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL: file ];
        documentInteractionController.UTI = @"net.whatsapp.movie";

        documentInteractionController.delegate = self;
        [documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
 }

Upvotes: 1

ali gurbuz
ali gurbuz

Reputation: 190

We have observed the exact same problem. Everything was working until a recent update of WhatsApp. This is probably a bug on WhatsApp side.

Here is a workaround for this problem:

  • Do not use a wam file, use the mp4 file directly. So in your case, just call

    [self shareVideoAtNativePath:nativePath SavePath:nativePath];
    
  • Change the UTI to public Mpeg4:

    self.documentInteractionController.UTI = @"public.mpeg-4";
    

This seems to have solved our issue. However, there is a drawback that, the share dialog now contains many other apps/services that can open mp4 files.

Upvotes: 1

Related Questions