ios-using afnetworking3.0+ (latest AFN)-Implement file breakpoint download

Source: Internet
Author: User
Tags uikit

0. Import Framework Preparation Work • 1. Drag and drop the afnetworking3.0+ framework program into the project ·2.  or use Cocopod to import afnetworking3.0+ 3. Introduction of #import "AFNetworking.h"

---->

1.UI Prep Work

A. Defining a global nsurlsessiondownloadtask: Download management handle It is responsible for all network operation requests.

1 @interface Viewcontroller () 2 {3     // Download handle 4     Nsurlsessiondownloadtask *_downloadtask; 5 }
1 . h file2 #import<UIKit/UIKit.h>3  4 @interfaceViewcontroller:uiviewcontroller5 //Download File Display6  7@property (Weak, nonatomic) Iboutlet Uiimageview *ImageView;8 //Download progress bar display9  Ten@property (Weak, nonatomic) Iboutlet Uiprogressview *Progressview; One @end A   - . m file - @interfaceViewcontroller () the { -     //Download handle -Nsurlsessiondownloadtask *_downloadtask; -}
2. Implement file download operation details with AFN
1- (void) downfilefromserver{2     3     //Remote Address4Nsurl *url = [Nsurl urlwithstring:@"Http://www.baidu.com/img/bdlogo.png"];5     //Default configuration6Nsurlsessionconfiguration *configuration =[Nsurlsessionconfiguration defaultsessionconfiguration];7     8     //afn3.0+ based on the handle of the closed urlsession9Afurlsessionmanager *manager =[[Afurlsessionmanager alloc] initwithsessionconfiguration:configuration];Ten      One     //Request ANsurlrequest *request =[Nsurlrequest Requestwithurl:url]; -      -     //Download Task Action the_downloadtask = [Manager downloadtaskwithrequest:request progress:^ (nsprogress *_nonnull downloadprogress) { -          -         //@property int64_t Totalunitcount; Total size of file to download -         //@property int64_t Completedunitcount; the size of the current download +          -         //Add monitoring KVO to progress +NSLog (@"%f",1.0* Downloadprogress.completedunitcount/downloadprogress.totalunitcount); A         //back to the home row refresh UI atDispatch_async (Dispatch_get_main_queue (), ^{ -         //set the percentage of the progress bar -   -Self.progressView.progress =1.0* Downloadprogress.completedunitcount/Downloadprogress.totalunitcount; -         }); -   in} Destination:^nsurl * _nonnull (Nsurl * _nonnull TargetPath, Nsurlresponse *_nonnull Response) { -          to         //-The return value of the block, which requires the return of a URL, which is the path to the location of the file +   -NSString *cachespath =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; theNSString *path =[Cachespath stringByAppendingPathComponent:response.suggestedFilename]; *         return[Nsurl Fileurlwithpath:path]; $  Panax Notoginseng} completionhandler:^ (Nsurlresponse * _nonnull response, Nsurl * _nullable FilePath, Nserror *_nullable Error) { -         //set up the download complete operation the         //filepath is where you download files, you can unzip them, or you can use them directly . +          ANSString *imgfilepath = [FilePath path];//turn the Nsurl into NSString theUIImage *img =[UIImage Imagewithcontentsoffile:imgfilepath]; +Self.imageView.image =img; -   $     }]; $}

3. With regard to the suspension and continuation
1 -(ibaction) Stopdownloadbtnclick: (ID) sender {2     // pause Download 3     [_downloadtask suspend]; 4 }5 -(ibaction) Startdownloadbtnclick: (ID) sender {6     // Start Download 7     [_downloadtask resume]; 8 }

4. Detect network status-optimize user experience
1- (void) Viewdidload {2 [Super Viewdidload];3     4     //Network Monitor handle5Afnetworkreachabilitymanager *manager =[Afnetworkreachabilitymanager Sharedmanager];6     7     //To Monitor network connection status, you must first invoke the Startmonitoring method of the singleton8 [manager startmonitoring];9     Ten[Manager setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) { One         //Status: A         //Afnetworkreachabilitystatusunknown =-1, Unknown -         //afnetworkreachabilitystatusnotreachable = 0, not connected -         //Afnetworkreachabilitystatusreachableviawwan = 1, 3G the         //Afnetworkreachabilitystatusreachableviawifi = 2, wireless connection -NSLog (@"%d", status); -     }]; -      +     //you are ready to download the file from remote. Click the Start button below to start the download task - [self downfilefromserver]; +   A}

Transferred from: http://www.cnblogs.com/qingche/p/5362592.html

ios-using afnetworking3.0+ (latest AFN)-Implement file breakpoint download

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.