Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Whatsapp发送图像和文本_Ios_Xcode_Swift_Whatsapp - Fatal编程技术网

Ios 使用Whatsapp发送图像和文本

Ios 使用Whatsapp发送图像和文本,ios,xcode,swift,whatsapp,Ios,Xcode,Swift,Whatsapp,我需要从我的应用程序发送一个带有文本的图像,我知道如何只发送图像或文本,但我不知道如何将两者结合起来 只是一个图像: let image = UIImage(named: "Image") // replace that with your UIImage let filename = "myimage.wai" let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .Us

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

只是一个图像:

    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交互:

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

您可以使用UIDocumentInteractionController,如2014年8月4日对该问题的第二次回答中所述:


希望这会有所帮助。

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)

即使只是分享一张图片也似乎不起作用,但可能会节省一些人的时间

没有任何东西可以将文本和媒体共享结合到whatsapp上