使用 Whatsapp 发送图像和文本

Ome*_* N. 5 xcode ios whatsapp swift

我需要从我的应用程序发送带有文本的图像,我知道如何仅发送图像或仅发送文本,但我不知道如何将两者结合起来。

只是一张图片:

    let image = UIImage(named: "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)    
Run Code Online (Sandbox Code Playgroud)

只是一段文字:

    var whatsappURL = NSURL(string: "whatsapp://send?text=hello,%20world")

    if UIApplication.sharedApplication().canOpenURL(whatsappURL!) {
        UIApplication.sharedApplication().openURL(whatsappURL!)
    }    
Run Code Online (Sandbox Code Playgroud)

如何发送带有文本的图像?

编辑#1

我找到了一个代码,可以将带有文本的图像共享到whatsapp,但它是用java编写的,你能将它翻译成swift吗?

Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.setType("image/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file)); //add image path
startActivity(Intent.createChooser(share, "Share image using"));
try {
    activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(activity, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}    
Run Code Online (Sandbox Code Playgroud)

Nir*_*raj 4

您可以在 WhatsApp 上发布图像或文本。但是,您不能同时发布两者,因为 Whatsapp 不提供任何可以添加标题和发布带有文本的图像的 API。

现在有一个可用于与 WhatsApp 交互的 api:

http://www.whatsapp.com/faq/en/iphone/23559013

另请参阅以下有用的答案:

您可以使用截至 2014 年 8 月 4 日此问题的第二个答案中提到的 UIDocumentInteractionController:在 iOS 应用程序中通过 WhatsApp 共享图像/文本

希望这会有所帮助。