ios - 使用 Whatsapp 发送图像和文本

标签 ios xcode swift whatsapp

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

只是一张图片:

    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)    

只是一个文本:

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

    if UIApplication.sharedApplication().canOpenURL(whatsappURL!) {
        UIApplication.sharedApplication().openURL(whatsappURL!)
    }    

如何发送带有文字的图片?

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

最佳答案

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

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

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

还可以在下面找到有用的答案:

您可以使用截至 2014 年 8 月 4 日此问题的第二个答案中提到的 UIDocumentInteractionController: Share image/text through WhatsApp in an iOS app

希望这会有所帮助。

关于ios - 使用 Whatsapp 发送图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920041/

相关文章:

ios - 一次引用 2 个不同 IBOutlet 的最有效方法(其中每个 IBOutlet 仅针对其自己的大小类别存在)?

swift - 如何在 Xcode 中添加可由 Swift 包管理器运行的可执行文件?

ios - 在 swift 中,为什么我必须使用 IBaction 或 IBOutlet 在代码和 UI 之间进行通信?

iphone - Apple 推送通知如何在 iPhone 应用程序中传递给用户?

ios - 以 iOS 9 作为部署目标的 Xcode 9.1 中的 iPhone 4s

ios - 如何在 Xcode 调试器中更改 "View Value as"的默认类型

swift - UIRefreshControl 故障与自定义 TableViewCell 相结合

ios - 尽管在 Controller 中设置 tabbar.hidden = NO,TabBar 仍然隐藏

ios - 无法在测试中将 App Delegate 转换为 App Delegate

xcode - 从 JavaScript 调用 swift 方法挂起 xcode 和应用程序