IOS File Download (afnetwork three-party framework with Progressview) five

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.