gpt4 book ai didi

iOS 在 Instagram 上的分享 : The file couldn’t be saved because the specified URL type isn’t supported

转载 作者:IT王子 更新时间:2023-10-29 05:28:18 28 4
gpt4 key购买 nike

我有以下代码可以在 swift 3 iOS 10.1 中在 Instagram 上分享图片:

func shareOnInstagram(_ photo: UIImage, text: String?) {
let instagramUrl = URL(string: "instagram://app")!
if UIApplication.shared.canOpenURL(instagramUrl) {
let imageData = UIImageJPEGRepresentation(photo, 1.0)!
let captionString = text ?? ""

let writePath = URL(string: NSTemporaryDirectory())!.appendingPathComponent("instagram.igo")
do {
try imageData.write(to: writePath)
let documentsInteractionsController = UIDocumentInteractionController(url: writePath)
documentsInteractionsController.delegate = self
documentsInteractionsController.uti = "com.instagram.exlusivegram"
documentsInteractionsController.annotation = ["InstagramCaption": captionString]
documentsInteractionsController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
}catch {
return
}
}else {
let alertController = UIAlertController(title: "Install Instagram", message: "You need Instagram app installed to use this feature. Do you want to install it now?", preferredStyle: .alert)
let installAction = UIAlertAction(title: "Install", style: .default, handler: { (action) in
//redirect to instagram
})
alertController.addAction(installAction)

let laterAction = UIAlertAction(title: "Later", style: .cancel, handler: nil)
alertController.addAction(laterAction)

present(alertController, animated: true, completion: nil)
}
}

try imageData.write(to: writePath) 这行,它抛出了如下错误:

Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported." UserInfo={NSURL=/private/var/mobile/Containers/Data/Application/5B80A983-5571-44A5-80D7-6A7B065800B5/tmp/instagram.igo}

有人可以帮我解决这个问题吗?

最佳答案

您正在错误地创建您的 URL。变化:

let writePath = URL(string: NSTemporaryDirectory())!.appendingPathComponent("instagram.igo")

到:

let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("instagram.igo")

无论何时从文件路径创建 URL,都必须使用 fileURLWithPath 初始值设定项。只有在创建以 httpmailto 等方案开头的 URL 时,使用 string 初始化程序才有效、tel 等 URL。

关于iOS 在 Instagram 上的分享 : The file couldn’t be saved because the specified URL type isn’t supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41640405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com