iOS 无法使用 UIDocumentInteractionController 将视频分享到 Whatsapp
iOS Could not share video to Whatsapp using UIDocumentInteractionController
它成功重定向到 Whatsapp app.There 没有预览帧,点击发送时,弹出错误消息“无法发送此视频。请选择其他视频。
这是我的代码。
- (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);
}
}
我们观察到了完全相同的问题。一切正常,直到最近更新了 WhatsApp。这可能是 WhatsApp 方面的错误。
以下是解决此问题的方法:
不要使用wam文件,直接使用mp4文件。所以在你的情况下,只需调用
[self shareVideoAtNativePath:nativePath SavePath:nativePath];
将 UTI 更改为 public Mpeg4:
self.documentInteractionController.UTI = @"public.mpeg-4";
这似乎解决了我们的问题。但是,有一个缺点,共享对话框现在包含许多其他可以打开 mp4 文件的apps/services。
我们遇到了完全相同的问题。
官方 .wam
格式仅在分享对话框中显示 WhatsApp,但无法转发视频。
使用 .m4v
格式对我们有用。与 WhatsApp 一起显示了更多选项(Open in WhatsApp
是我们想要的选项)。在将用户发送到共享对话框之前,我们显示一条警告说 "Please select Whatsapp on the next screen"。
文件格式:m4v
尿路感染: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];
}
// 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];
它成功重定向到 Whatsapp app.There 没有预览帧,点击发送时,弹出错误消息“无法发送此视频。请选择其他视频。
这是我的代码。
- (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);
}
}
我们观察到了完全相同的问题。一切正常,直到最近更新了 WhatsApp。这可能是 WhatsApp 方面的错误。
以下是解决此问题的方法:
不要使用wam文件,直接使用mp4文件。所以在你的情况下,只需调用
[self shareVideoAtNativePath:nativePath SavePath:nativePath];
将 UTI 更改为 public Mpeg4:
self.documentInteractionController.UTI = @"public.mpeg-4";
这似乎解决了我们的问题。但是,有一个缺点,共享对话框现在包含许多其他可以打开 mp4 文件的apps/services。
我们遇到了完全相同的问题。
官方 .wam
格式仅在分享对话框中显示 WhatsApp,但无法转发视频。
使用 .m4v
格式对我们有用。与 WhatsApp 一起显示了更多选项(Open in WhatsApp
是我们想要的选项)。在将用户发送到共享对话框之前,我们显示一条警告说 "Please select Whatsapp on the next screen"。
文件格式:m4v
尿路感染: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];
}
// 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];