当前位置:  开发笔记 > 编程语言 > 正文

Instagram上的iOS共享:无法保存文件,因为不支持指定的URL类型

如何解决《Instagram上的iOS共享:无法保存文件,因为不支持指定的URL类型》经验,为你挑选了1个好方法。

我有以下代码在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}

有人可以帮我弄这个吗?



1> rmaddy..:

您正在创建URL错误的.更改:

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

至:

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

无论何时URL从文件路径创建,都必须使用fileURLWithPath初始化程序.使用string初始化时创建一个仅对URL与一个方案开始,如http,mailto,tel等网址.

推荐阅读
重庆制造漫画社
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有