【问题标题】:how to download pdf file from json in swift and alamofire in ios?如何在ios中从swift和alamofire中的json下载pdf文件?
【发布时间】:2017-09-14 04:55:35
【问题描述】:

如何使用 Swift 3.0 和 Alamofire 在 IOS 中下载 PDF 文件。我可以使用 nsurlsession 获取 url。但我正在寻找 alamofire 代码。 请看我的代码。

func downloadPdffile(_ sender : UIButton) {
    print(sender.tag)
    print("ARRAY VALUES FROM CELL",totalSyllabusArray.object(at: sender.tag))
    var localDic :NSDictionary!
    localDic = totalSyllabusArray.object(at: sender.tag) as! NSDictionary
    let filepath = localDic["filepath"] as! String
    print("pressed ")
    let strURL1:String = FETCH_InMegh_Image_BaseURL + filepath
    print("strURL1 is ",strURL1)
    let pathURL = URL(string: strURL1)!
    let sessionConfig = URLSessionConfiguration.default
    let session = URLSession(configuration: sessionConfig)
    let request = try! URLRequest(url: pathURL, method: .get)
    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("Success: \(statusCode)")
                print("tempLocalUrl: \(tempLocalUrl)")
                              } else {
                print("Failure: %@", error?.localizedDescription);
            }
        }
    }
}

}

【问题讨论】:

  • 如何提问。您可以自行搜索。但是您要的是教程、库,这不是一个可以接受的问题。
  • 我没有得到你@ElTomato

标签: ios json swift pdf alamofire


【解决方案1】:

定义你的 destination 类似的东西:

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let fileURL = documentsURL.appendingPathComponent("your.pdf")
    return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}  

并使用您的网址和目的地致电Alamofire.download

Alamofire.download(yourUrl), to: destination).response { response in
        let parentView = (self.superview?.superview as! UITableView).dataSource as! ProcedureViewController
        parentView.hideActivityIndicator()
        if response.error == nil, let _ = response.destinationURL?.path {
            //open pdf in UIDocumentInteractionController
            self.docController = UIDocumentInteractionController.init(url: response.destinationURL!)
            self.docController?.delegate = self.delegate!
            self.docController?.name = ""
            self.docController?.presentOptionsMenu(from: self.parentView!.bounds, in: self.parentView!, animated: true)
        }
    }

【讨论】:

  • 你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 2023-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多