xcode 使用 Whatsapp 发送图像和文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31920041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:39:15  来源:igfitidea点击:

Send Image and Text With Whatsapp

iosxcodeswiftwhatsapp

提问by Omer N.

I need to send an image from my app with a text, I know how to send just an image or just a text, but I don't know how to combine both of them.

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

Just an Image:

只是一个图像:

    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)    

Just a text:

只是一段文字:

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

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

How can I send an image with a text?

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

EDIT #1

编辑#1

I found a code that share an image with text to whatsapp but it's in java, can you translate it to swift?

我找到了一个代码,可以将带有文本的图像共享到 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();
}    

回答by Niraj

You can post Image or Text on WhatsApp. However you can't post both at a time as whatsapp does not provide any API that you can add caption and post image with text.

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

Now there is an api available for interacting with WhatsApp:

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

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

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

Also Find below helpful answer:

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

You can use the UIDocumentInteractionController as mentioned in the 2nd answer to this question as of August 4, 2014: Share image/text through WhatsApp in an iOS app

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

Hope this will help.

希望这会有所帮助。

回答by jugutier

A version of your share image code for swift 3:

swift 3 的共享图像代码的一个版本:

let image = myUIImageVariable
        let filename = "myimage.wai"
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, false)[0] as NSString
        var destinationPath = documentsPath.appending("/" + filename) as NSString
         destinationPath = destinationPath.expandingTildeInPath as NSString

        let fileUrl = NSURL(fileURLWithPath: destinationPath as String) as NSURL
        do{
            try UIImagePNGRepresentation(image!)?.write(to: fileUrl as URL, options: Data.WritingOptions.atomic)
        }
        catch {}
        let documentController = UIDocumentInteractionController(url: fileUrl as URL)
        documentController.delegate = self
        documentController.uti = "net.whatsapp.image"
        documentController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: false)

Still does not seem work even for just sharing an image, but may save someone's time

即使只是共享图像似乎仍然不起作用,但可能会节省某人的时间