iOS无法使用UIDocumentInteractionController与Whatsapp共享视频

时间:2015-10-29 04:48:46

标签: ios video whatsapp

它成功重定向到Whatsapp应用程序。没有预览框架,当点击发送时,弹出错误消息"无法发送此视频。请选择其他视频。

这是我的代码。

- (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);
}
}

3 个答案:

答案 0 :(得分:1)

我们已经观察到完全相同的问题。在最近更新WhatsApp之前,一切正常。这可能是WhatsApp方面的一个错误。

以下是此问题的解决方法:

  • 请勿使用wam文件,请直接使用mp4文件。所以在你的情况下,只需致电

    [self shareVideoAtNativePath:nativePath SavePath:nativePath];
    
  • 将UTI更改为公共Mpeg4:

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

这似乎解决了我们的问题。但是,有一个缺点是,共享对话框现在包含许多可以打开mp4文件的其他应用程序/服务。

答案 1 :(得分:1)

我们遇到了完全相同的问题。

官方.wam格式仅在共享对话框中显示WhatsApp,但无法转发视频。

使用.m4v格式对我们有用。还有一些选项与WhatsApp一起显示(Open in WhatsApp是我们想要的选项)。在将用户发送到共享对话框之前,我们正在显示警告“请在下一个屏幕上选择Whatsapp”。

文件格式:m4v

UTI:net.whatsapp.movie

请参阅下面的工作代码:

 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];
 }

答案 2 :(得分:0)

GC_MALLOC