2013-10-22 37 views
2

2013年7月,WhatsApp为我们的应用程序打开了他们的URL方案。我从我的应用程序发送文本到WhatsApp,但现在我想发送一个图像。如何将图像发送到WhatsApp?如何从我的应用程序发送图像到WhatsApp?

我不知道它是如何做到的。

谢谢。

+1

http://www.whatsapp.com/faq/en/iphone/23559013 如果我正确地理解他们,只是让UIDocumentInteractionController的一个实例,并且图像初始化它然后应该可以通过UIDocumentInteractionController的内置操作按钮发送图像。 – Marc

+0

非常感谢。我尝试使用UIDocumentInteracionController,并通过WhatsApp发送图像。 – Paolpa

回答

5

根据他们的documentation,您需要使用UIDocumentInteractionController。要在文档控制器选择性地仅显示了WhatsApp(它呈现给用户,在这一点上,他们可以选择的WhatsApp分享给),按照他们的指示:

另外,如果你想只显示了WhatsApp在应用程序列表(代替了WhatsApp和任何其他公共/ * - 符合应用程序),您可以指定保存是独家WhatsApp的扩展上述类型之一的文件:

images - «.wai» which is of type net.whatsapp.image 
videos - «.wam» which is of type net.whatsapp.movie 
audio files - «.waa» which is of type net.whatsapp.audio 

您需要保存映像到磁盘,然后使用该文件URL创建一个UIDocumentInteractionController

下面是一些示例代码:

_documentController = [UIDocumentInteractionController interactionControllerWithURL:_imageFileURL]; 
_documentController.delegate = self; 
_documentController.UTI = @"net.whatsapp.image"; 
[_documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES] 
+0

非常感谢。我尝试使用UIDocumentInteracionController,并通过WhatsApp发送图像。 – Paolpa

+0

是否有可能在一个共享中共享图像和文本? – Vaiden

+0

根据这篇文章,自11月以来,你可以做到这一点。这是解释如何的帖子。 http://stackoverflow.com/questions/23077338/share-image-and-text-through-whatsapp-or-facebook 这是Android的发展,请让我知道如果你想我写它的Obj -C /夫特。 –

2

下面是一个斯威夫特最终工作溶液。该方法由一个按钮触发。你可以找到一些解释here

import UIKit 

class ShareToWhatsappViewController: UIViewController, UIDocumentInteractionControllerDelegate { 
    var documentController: UIDocumentInteractionController! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    @IBAction func shareToWhatsapp(sender: UIButton) { 
     let image = UIImage(named: "my_image") // replace that with your UIImage 

     let filename = "myimage.wai" 
     let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as! NSString 
     let destinationPath = documentsPath.stringByAppendingString("/" + filename).stringByExpandingTildeInPath 
     UIImagePNGRepresentation(image).writeToFile(destinationPath, atomically: false) 
     let fileUrl = NSURL(fileURLWithPath: destinationPath)! as NSURL 

     documentController = UIDocumentInteractionController(URL: fileUrl) 
     documentController.delegate = self 
     documentController.UTI = "net.whatsapp.image" 
     documentController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: false) 
    } 
} 
相关问题