how to download pdf file from json in swift and alamofire in ios? -
how download pdf file in ios using swift 3.0 , alamofire . can able fetch url nsurlsession. looking alamofire code. please see code.
func downloadpdffile(_ sender : uibutton) { print(sender.tag) print("array values 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 ",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); } } } }
}
define destination
that:
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]) }
and call alamofire.download
url , destination:
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) } }
Comments
Post a Comment