首页 > 解决方案 > 通过 URL 在 Whatsapp 上分享图像

问题描述

我正在使用 json 从我的 WordPress 网站创建图像应用程序,我正在使用 swift,我想从我的应用程序在 whatsapp 上共享图像,目前我尝试了这个代码它可以工作但只有图像名称我想从图像 url 共享图像,是那可能吗?

这是我的代码

 let urlWhats = "whatsapp://app"
          if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
              if let whatsappURL = URL(string: urlString) {

                  if UIApplication.shared.canOpenURL(whatsappURL as URL) {

                      if let image = UIImage(named: "splash") {
                        if let imageData = image.jpegData(compressionQuality: 1.0) {
                              let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
                              do {
                                  try imageData.write(to: tempFile, options: .atomic)
                                  self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
                                  self.documentInteractionController.uti = "net.whatsapp.image"
                                  self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)

                              } catch {
                                  print(error)
                              }
                          }
                      }

                  } else {
                      // Cannot open whatsapp
                  }
              }
          }

谢谢

标签: iosswiftxcodesharewhatsapp

解决方案


首先,您应该从 URL 下载图像

  • 创建一个函数来获取数据
func data(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
    URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}

为此该怎么办->

let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
    if let whatsappURL = URL(string: urlString) {

        if UIApplication.shared.canOpenURL(whatsappURL as URL) {

            // Set your image's URL into here
            let url = URL(string: "https://your-image-url.com")!
            data(from: url) { data, response, error in
                guard let data = data, error == nil else { return }
                DispatchQueue.main.async() { [weak self] in

                    let image = UIImage(data: data)
                    if let imageData = image.jpegData(compressionQuality: 1.0) {
                        let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
                        do {
                            try imageData.write(to: tempFile, options: .atomic)
                            self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
                            self.documentInteractionController.uti = "net.whatsapp.image"
                            self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)

                        } catch {
                            print(error)
                        }
                    }
                }
            }

        } else {
            // Cannot open whatsapp
        }
    }
}

推荐阅读