Afurlsessionmanager can definitely be called the core of afnetworking. Responsible for creating and managing Nsurlsession Management Nsurlsessiontask Implementing proxy methods in Nsurlsessiondelegate and other protocols use Afurlsessionmanagertaskdelegate manage progress using _ Afurlsessiontaskswizzling Transfer method introduces Afsecuritypolicy guarantee Request Security Introduction Afnetworkreachabilitymanager Monitor network status
1. Create
In the initialization method, you need to complete initializing some of your own instances: Initialize session configuration (nsurlsessionconfiguration), default to Defaultsessionconfiguration initialization session, and set the agent for the session and the agent queue Initialization Management response serialization (Afjsonresponseserializer), Security Authentication (Afsecuritypolicy) and instance initialization of monitoring network status (Afnetworkreachabilitymanager) Save Data task 's Dictionary (mutabletaskdelegateskeyedbytaskidentifier)
-(Instancetype) Initwithsessionconfiguration: (nsurlsessionconfiguration *) configuration {self = [super init]; if (!self) {return nil; } if (!configuration) {configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; } self.sessionconfiguration = Configuration; Self.operationqueue = [[Nsoperationqueue alloc] init]; Self.operationQueue.maxConcurrentOperationCount = 1; Self.session = [Nsurlsession sessionWithConfiguration:self.sessionConfiguration delegate:self Delegatequeue: Self.operationqueue]; Self.responseserializer = [Afjsonresponseserializer serializer]; Self.securitypolicy = [Afsecuritypolicy defaultpolicy]; Self.reachabilitymanager = [Afnetworkreachabilitymanager Sharedmanager]; Self.mutabletaskdelegateskeyedbytaskidentifier = [[Nsmutabledictionary alloc] init]; Self.lock = [[Nslock alloc] init]; Self.lock.name = Afurlsessionmanagerlockname; #1: Set the agent for the existing task and return to self;}
2. Management
-(Nsurlsessiondatatask *) Datataskwithrequest: (nsurlrequest *) Request uploadprogress: (Nullab Le void (^) (nsprogress *uploadprogress)) Uploadprogressblock downloadprogress: (Nullable void ( ^) (nsprogress *downloadprogress)) Downloadprogressblock Completionhandler: (Nullable Void (^) (NS Urlresponse *response, id _nullable responseobject, nserror * _nullable error)) completionhandler;-( Nsurlsessionuploadtask *) Uploadtaskwithrequest: (nsurlrequest *) Request FromFile: (N sURL *) FileURL progress: (Nullable void (^) (nsprogress *uploadprogress)) Uploadprog Ressblock Completionhandler: (Nullable Void (^) (nsurlresponse *response, id _nullable respon Seobject, Nserror * _nullable error)) completionhandler;...-(Nsurlsessiondownloadtask *) Downloadtaskwithrequest: ( Nsurlrequest *) Request Progress: (Nullable void (^) (nsprogress *downloadprogress)) Downloadprogressblock Destination: (Nullable Nsurl * (^) (Nsurl *targetpath, Nsurlresponse *response)) destination Completionhandler: (Nullable Void (^) (Nsurlresponse *response, Nsurl * _nullable filePath, Nserror * _null Able error)) Completionhandler;
-(Nsurlsessiondatatask *) Datataskwithrequest: (nsurlrequest *) Request uploadprogress: (Nullable Void (^) ( Nsprogress *uploadprogress)) Uploadprogressblock downloadprogress: (Nullable Void (^) (nsprogress * downloadprogress)) Downloadprogressblock Completionhandler: (Nullable Void (^) (Nsurlresponse *response, id _ Nullable Responseobject, nserror * _nullable error)) Completionhandler { __block nsurlsessiondatatask * Datatask = nil; url_session_manager_create_task_safely (^{ datatask = [self.session datataskwithrequest:request]; }); [Self adddelegatefordatatask:datatask uploadprogress:uploadprogressblock Downloadprogress:downloadprogressblock Completionhandler:completionhandler]; return datatask;}
Call-[Nsurlsession Datataskwithrequest:] Method incoming Nsurlrequest call-[Afurlsessionmanager adddelegatefordatatask: UploadProgress:downloadProgress:completionHandler:] method returns a Afurlsessionmanagertaskdelegate object that will Completionhandler Uploadprogressblock and Downloadprogressblock pass in the object and make callbacks when the corresponding event occurs
3. Realizing the delegate of Nsurlsesstion
-(void) Setsessiondidbecomeinvalidblock: (Nullable Void (^) (nsurlsession *session, Nserror *error)) block;-(void) Setsessiondidreceiveauthenticationchallengeblock: (Nullable Nsurlsessionauthchallengedisposition (^) (NSURLSession *session, Nsurlauthenticationchallenge *challenge, nsurlcredential * _nullable __autoreleasing * _Nullable credential)) Block, .....
-(void) Setsessiondidbecomeinvalidblock: (void (^) (nsurlsession *session, Nserror *error)) block { Self.sessiondidbecomeinvalid = Block;}
IOS Development Network Detailed (11) Afurlsessionmanager