Swift 3中文件下载的间歇性问题

时间:2017-11-16 07:52:27

标签: ios swift

我有时无法下载文件,有时它会下载或者有些错误。这是我的代码请帮忙:

let urlGetStr = urlStr + "?&requestSource=Mobile&requestData="+inputData;
        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)
        var request = try! URLRequest(url: URL(string: baseUrl + urlGetStr)!);
        request.httpMethod = "GET";
        //Create URL to the source file you want to download
        //let session = URLSession.shared
        session.downloadTask(with: request) { (tempLocalUrl, response, error) in
            DispatchQueue.main.async
                {
                    indicator.stopAnimating();
                    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: localFileStr)
                        self.doAfterFileDownload(localFileStr, viewController: viewController);
                    } catch (let writeError) {
                        print("Error creating a file \(localFileStr) : \(writeError)")
                        bondEvalueCommonView.showAlertDialog("Alert", message : "Unable to downlaod the document. Please try again after sometime or contact the support team.", btnText : "OK");
                    }
                } else {
                    print("Error took place while downloading a file. Error description: %@", error?.localizedDescription as Any);
                }
            }
        }.resume()
    }

0 个答案:

没有答案