1. Create a request
-(void) download2{ nsstring *urlstring = @ "Http:192.168.0.179:8080/myweb/download.do"; URLString = [URLString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; NSLog (@ "Hello"); Nsurl *url = [Nsurl urlwithstring:urlstring]; Nsurlrequest *request = [Nsurlrequest requestwithurl:url]; [[Self.session downloadtaskwithrequest:request] resume]; }
One last sentence
[[Self.session downloadtaskwithrequest:request] resume];
The session, this article uses lazy loading
2. Session lazy loading, and add agent, listen to file download situation
Lazy Loading-(nsurlsession *) session{ if (_session = = nil) { nsurlsessionconfiguration *config = [ Nsurlsessionconfiguration Defaultsessionconfiguration]; _session = [nsurlsession sessionwithconfiguration:config delegate:self delegatequeue:nil]; } return _session;}
3. Progress update and label download percent complete update (note that it must be updated on the main thread, otherwise it cannot be displayed)
/* Sent periodically to notify the delegate of download progress. *//**[""] * @brief update progress bar, use this agent [""] *[""] * @param session session[""] * @param downloadtask download task [""] * @param byteswritten Current Write Bytes[""] * @param totalbyteswritten Current total write bytes[""] * @param totalbytesexpectedtowrite expected write total bytes[""] *[""] * @return < #return value description#>[""] */-(void) Urlsession: (Nsurlsession *) session Downloadtask: ( Nsurlsessiondownloadtask *) Downloadtask didwritedata: (int64_t) Byteswritten Totalbyteswritten: (int64_t) Totalbyteswrittentotalbytesexpectedtowrite: (int64_t) totalbytesexpectedtowrite{ Float progress = (float) Totalbyteswritten/totalbytesexpectedtowrite; NSLog (@ "%f", progress); Main thread Update UI Dispatch_async (Dispatch_get_main_queue (), ^ (void) { self.progresslabel.text=[nsstring stringwithformat:@ "%%%.0f", progress*100]; [Self.progressview setprogress:progress animated:yes]; }
4. The download is complete and the downloaded data is written to the specified cache path.
/** [""] * @briefThe delegate should copy or move the file at the given location to a new location as it would be Removed when the delegate message returns. [""] *[""] * @param session session description[""] * @param downloadtask downloadtask Download task [""] * @param location Download document position (temporary) [ ""] *[""] * @return [""] */-(void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtaskdidfinishdownloadingtourl: (Nsurl *) location{nsstring *cachedir = Nssearchpathfordirectoriesindomains ( Nscachesdirectory, Nsuserdomainmask, YES) [0]; NSString *retstr = [DownloadTask.response.suggestedFilename stringbyreplacingpercentescapesusingencoding: Nsutf8stringencoding]; NSLog (@ "%@ \ n", retstr); NSString *path = [Cachedir stringbyappendingpathcomponent:retstr]; NSLog (@ "%@", Path); Nsurl *url2=location; nslog (@ "%@", location); NSData *mydata=[nsdata Datawithcontentsofurl:location]; [MyData Writetofile:path atomically:yes];}
IOS File Download (afnetwork three-party framework with Progressview) five