Nsurlsession Request encapsulation Plus HUD loading box

Source: Internet
Author: User


The method inside. h defines a singleton class typedef void (^successblock) (ID block), typedef void (^failureblock) (ID block), @interface Netrequest: NSObject <nsurlsessiondelegate>+ (instancetype) shareinstance;-(void) Requestwithurl: (NSString *) URL params: ( Nsdictionary *) params successblock: (successblock) Success failure: (Failureblock) failure;

#import <SVProgressHUD.h>//third-party load bar control static netrequest *reuqest; @implementation netrequest{Successblock Sucbloc    K    Failureblock Faiblock; Nsmutabledata *_alldata;}    + (instancetype) shareinstance{static dispatch_once_t Oncetoken;    Dispatch_once (&oncetoken, ^{reuqest = [[Netrequest alloc] init];    }); return reuqest;}    + (Instancetype) Allocwithzone: (struct _nszone *) zone{if (!reuqest) {reuqest = [super Allocwithzone:zone]; } return reuqest;} -(ID) copy{return reuqest;} -(void) Requestwithurl: (nsstring *) URL params: (nsdictionary *) params successblock: (successblock) Success failure: ( Failureblock) failure{nsurlrequest *myrequest = [nsurlrequest requestwithurl:[nsurl URLWithString:url] CachePolicy:            Nsurlrequestuseprotocolcachepolicy timeoutinterval:15]; Nsurlsessiondownloadtask *download =[[nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration] Delegate:self Delegatequeue:[nsoperationqueue Mainqueue]] downloadtaskwithrequest:myrequest]; Start request [Svprogresshud showwithstatus:@ "Loading ..."];            Prompt to load [downLoad resume];    Sucblock = success;    Faiblock = failure; }//Request Complete-(void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *)    Downloadtaskdidfinishdownloadingtourl: (Nsurl *) location{nsdata *data = [NSData datawithcontentsofurl:location];    ID reuslt = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil];    Sucblock (REUSLT); [Svprogresshud dismiss];} Network Request Progress-(void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask Didwriteda TA: (int64_t) Byteswritten Totalbyteswritten: (int64_t) Totalbyteswrittentotalbytesexpectedtowrite: (int64_t) totalbytesexpectedtowrite{Double progress = (double) totalbyteswritten/totalbytesexpectedtowr        Ite    NSLog (@ "Download Progress---%f", progress); [Svprogresshud showprogress:p rogress]; }

And then you can write it in Viewcontroller.

Request    NSString *string = [nsstring stringwithformat:@ "/HTTP [/...] .... ........ ...];    ............. Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:string];        Configuration    nsurlsession *session = [Nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration]];        Task    Nsurlsessiontask *task = [Session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {        nsdictionary *result = [Nsjsonserialization JSONObjec Twithdata:data options:nsjsonreadingmutablecontainers Error:nil];        NSLog (@ "%@", result); }

In general, we will encapsulate a AFN network request to meet our basic needs but occasionally we have to use some

Nsurlsession  If the special case is encapsulated, you can use it later.

Nsurlsession Request encapsulation Plus HUD loading box

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.