【问题标题】:Download and save pdf into files from url in swift快速从url下载pdf并将其保存到文件中
【发布时间】:2021-01-21 05:59:07
【问题描述】:

我正在从 url 下载 pdf 并保存到 .documentsDirectory。但它保存在应用数据中的某个地方,而不是我想将它保存在手机上。

    func downnload(url: NSURL, filename:String) {
        
        let fileName = String((url.lastPathComponent!)) as NSString
        let documentsUrl:URL =  (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL?)!
        let destinationFileUrl = documentsUrl.appendingPathComponent("\(fileName)")
        
        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)
        let request = URLRequest(url:url as URL)
        let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
            if let tempLocalUrl = tempLocalUrl, error == nil {
                // Success
                if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                    print("Successfully downloaded. Status code: \(statusCode)")
                }
                do {
                    try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                    do {
                        let contents  = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
                        for indexx in 0..<contents.count {
                            if contents[indexx].lastPathComponent == destinationFileUrl.lastPathComponent {
                                let activityViewController = UIActivityViewController(activityItems: [contents[indexx]], applicationActivities: nil)
                                self.present(activityViewController, animated: true, completion: nil)
                            }
                        }
                    }
                    catch (let err) {
                        print("error: \(err)")
                    }
                } catch (let writeError) {
                    print("Error creating a file \(destinationFileUrl) : \(writeError)")
                }
            } else {
                print("Error took place while downloading a file. Error description: \(error?.localizedDescription ?? "")")
            }
        }
        task.resume()
    }
}

【问题讨论】:

    标签: ios swift pdf


    【解决方案1】:

    您可以使用 UIDocumentInteractionController 将文件保存到 Apple 的 Files 应用程序中

    从 url 下载 pdf 并保存到 .documentsDirectory 后,将您的 documentsDirectory 文件 url 提供给 UIDocumentInteractionController

    let documentInteractionController = UIDocumentInteractionController()
    
    documentInteractionController.url = url //Here is your documentsDirectory file url 
    documentInteractionController.uti = url.typeIdentifier ?? "public.data, public.content"
    documentInteractionController.name = url.localizedName ?? url.lastPathComponent
    documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
    

    这里我使用的是 URL 扩展

    extension URL {
      var typeIdentifier: String? {
        return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier
      }
      var localizedName: String? {
        return (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2019-11-18
      • 2020-05-30
      • 2011-01-31
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多