【问题标题】:Whatsapp Image sharing not workingWhatsapp 图像共享不起作用
【发布时间】:2014-12-09 13:56:09
【问题描述】:

我的班级实现了UIDocumentInteractionControllerDelegate,我有以下属性:@property (nonatomic,retain) UIDocumentInteractionController * documentInteractionController;

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) {
    UIImage     *iconImage = [UIImage imageNamed:@"bg_iPhone_5.jpg"];
    NSString    *savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

    [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
    _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
    _documentInteractionController.UTI = @"net.whatsapp.image";
    _documentInteractionController.delegate = self;
    [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
}
else {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

您好,我已使用此代码,但图像共享不起作用。 DocumentInteractionController 启动一个带有 whatsapp 图标的屏幕,当我点击它时,我的应用程序崩溃了。

【问题讨论】:

  • launchServices: 调用了 invalidationHandler。我在调试区得到了这个。
  • 您是否调试过代码,例如确保图像已写入?因为你根本不检查这个。另外,为了获取文档目录,添加到 NSHomeDirectory() 并不是真正正确的。使用URLForDirectory:inDomain:appropriateForURL:create:error:
  • 你能告诉我使用NSHomeDirectory的确切方法吗?
  • 看我的回答,这应该对你有帮助。如果任何NSLog 被触发,您将在调试输出中记录错误,这将告诉您更多关于在哪里可以找到您的问题
  • 我把解决方案放在这里stackoverflow.com/questions/8354417/…

标签: ios image whatsapp uidocumentinteraction


【解决方案1】:

这里是设置用于写出临时图像的 URL 的正确方法的代码:

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) {
    NSError *error = nil;
    NSURL *documentURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];
    if (!documentURL) {
        NSLog(@"Error getting doucment directory: %@", error);
    }

    NSURL *tempFile = [documentURL URLByAppendingPathComponent:@"whatsAppTmp.wai"];
    UIImage *iconImage = [UIImage imageNamed:@"bg_iPhone_5.jpg"];

    NSData *imageData = UIImageJPEGRepresentation(iconImage, 1.0) ;
    if (![imageData writeToURL:tempFile options:NSDataWritingAtomic error:&error]) {
        NSLog(@"Error writing File: %@", error);
    }

    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:tempFile];
    self.documentInteractionController.UTI = @"net.whatsapp.image";
    self.documentInteractionController.delegate = self;
    [self.documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
}
else {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

【讨论】:

  • 非常感谢。完美运行!
  • 嗨,如何在分享图片时添加文字
  • 我想分享一个与图片相关的链接,请帮忙
【解决方案2】:

要使用 WhatsApp 共享图像,请使用以下代码。它工作正常。

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){

        UIImage     * iconImage = [UIImage imageNamed:@"my_account.png"];
        NSString    * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

        [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];

        _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
        _documentInteractionController.UTI = @"net.whatsapp.image";
        _documentInteractionController.delegate = self;

        [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];


    } else {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

【讨论】:

    【解决方案3】:

    对我来说,当我使用 documentInteractionController 作为属性时,问题得到了解决。之前我在 share 方法中使用它作为局部变量。

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多