【问题标题】:How to store pdf from a url in phone memory [closed]如何从手机内存中的url存储pdf [关闭]
【发布时间】:2019-10-08 17:20:23
【问题描述】:

我有 pdf 网址,我想知道如何将该 pdf 存储在我的手机内存中的代码。我想存储该 pdf 并希望通过手机查看该 pdf。我正在使用 swift 4 和 Xcode 进行编码

【问题讨论】:

标签: swift


【解决方案1】:

斯威夫特 4

调用这个函数只传url,下载后会返回文件url........

func get_file(httpUrl:String)
    {
        let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let url = URL(string: httpUrl)!
        let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)

        let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil)
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        let task = session.dataTask(with: request, completionHandler:
        {
            data, response, error in
            if error == nil
            {
                if let response = response as? HTTPURLResponse
                {
                    if response.statusCode == 200
                    {
                        if let data = data
                        {
                            if let _ = try? data.write(to: destinationUrl, options: Data.WritingOptions.atomic)
                            {
                                print(destinationUrl.absoluteString)
                            }
                            else
                            {
                                print(error!)
                            }
                        }
                        else
                        {
                            print(error!)
                        }
                    }
                }
            }
            else
            {
                print(error!)
            }
        })
        task.resume()

    }

你在这里找到了你的文件..

let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let url = URL(string: file_str)!
        let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)

【讨论】:

    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 2021-09-12
    • 2019-06-09
    • 1970-01-01
    • 2013-01-12
    • 2017-06-06
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多