我需要从我的应用程序发送带有文本的图像,我知道如何只发送图像或文本,但我不知道如何将两者结合起来。
只是一张图片:
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/