Rare once notes quite detailed native get network request operation, like obsessive-compulsive disorder recorded and shared ... Also ready for replication
-(void) getresult{_MB = [Mbprogresshud showhudaddedto: Self. Navigationcontroller. ViewAnimated:YES];//interface path NSString*path = @"Http://a.apix.cn/apixlife/phone/phone";//path-+ parameter NSString*pathwithphonenum = [NSStringstringwithformat:@"%@?phone=%@", PATH,_PHONENUMFD. Text];//Chinese code NSString*urlpath = [Pathwithphonenum stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset Urlqueryallowedcharacterset]];//url Nsurl*phoneurl = [NsurlUrlwithstring:urlpath];//Request ObjectNsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:phoneurl];//Request Method[Request sethttpmethod:@"GET"];//Request Header[Request setvalue:@"92b5787ecd17417b718a2aaedc7e6ce8"forhttpheaderfield:@"Apix-key"];//Network configurationNsurlsessionconfiguration *configuration = [Nsurlsessionconfiguration defaultsessionconfiguration];//Network sessionNsurlsession *session = [Nsurlsession sessionwithconfiguration:configuration];//TasksNsurlsessiondatatask *sessiontask = [Session datataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable Response,Nserror* _nullable error) {//Return to the main thread update UI, Undo Matte Dispatch_async(Dispatch_get_main_queue (), ^{[_mb Hide:YES]; });if(Error) {NSLog(@"Request failed ...%@", error);//Prompt user request failed!Uialertcontroller *av = [Uialertcontroller alertcontrollerwithtitle:@"Hint"message:@"Sorry, server error, please try again later ..."Preferredstyle:uialertcontrollerstyleactionsheet]; [AV addaction:[uialertaction actionwithtitle:@"OK"Style:uialertactionstyledefault handler:^ (uialertaction * _nonnull action) {//Click OK to perform the operation, you can place nil NSLog(@"You clicked OK ..."); }]]; [ SelfPresentviewcontroller:av Animated:YESCompletion:Nil]; }Else{//json Analysis of Apple native efficiency highest nsdictionary*result = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:Nil];if([[Result objectforkey:@"Message"] isequaltostring:@"Success"]) {//Get data--Main thread update UI Dispatch_async(Dispatch_get_main_queue (), ^{nsdictionary*data = [Result objectforkey:@"Data"];NSString*city = [Data objectforkey:@"City"];NSString*province = [Data objectforkey:@"province"];NSString*belong = [NSStringstringwithformat:@"%@ · %@", province,city]; [_resultlb Settext:belong]; }); }Else{NSLog(@"No information found ..."); }NSLog(@"Request succeeded ...%@", result); } }];//Start Task[Sessiontask resume];}
Ios-get Request verbose Operation-get Set the request header