ios-与Twitter分享图片

时间:2019-05-08 11:28:53

标签: ios twitter

为了避免停产的Twitter Kit,我想使用canOpenURL方法共享一个Image对象。

这是我的代码,但是不起作用。

是否只能与Twitter应用共享图像?

func shareTwitter(image: UIImage, result: ((Bool) -> Void)? = nil) {
        guard let url = NSURL(string: "twitter://app") else {
            if let result = result {
                result(false)
            }
            return
        }

        let image = image.scaleImageWithAspectToWidth(toWidth: 640)

        do {
            try PHPhotoLibrary.shared().performChangesAndWait {
                let request = PHAssetChangeRequest.creationRequestForAsset(from: image)

                let assetID = request.placeholderForCreatedAsset?.localIdentifier ?? ""
                let shareURL = "twitter://post?message=hello%20world&media_id=" + assetID

                if UIApplication.shared.canOpenURL(url as URL) {
                    if let urlForRedirect = NSURL(string: shareURL) {
                        UIApplication.shared.open(urlForRedirect as URL, options: [:], completionHandler: nil)
                    }
                }
            }
        } catch {
            if let result = result {
                result(false)
            }
        }
    }

0 个答案:

没有答案