Objective
In view of the Chai Jing version "under the Sky" of the hot, perhaps more people will care about the environment, air quality and other environmental factors, reference to the swift version of a college PM2.5, I wrote a objective-c version of the.
Basic principle
Get current air quality data from the server, Weather Center open Interface, Asian air quality real-time monitoring, reference to others ' tutorials using Asian air quality monitoring.
Parses the data locally, obtains PM2.5 values, and displays them.
Specific implementation
NSNumber*str;-(void) loaddata{ Self. Msgtext. Text= @"Loading ...";Nsurl*url = [Nsurlurlwithstring:@"Http://aqicn.org/publishingdata/json"];NSString*[email protected]"PostData"; NSData *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:YES]; Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; [Request sethttpmethod:@"POST"]; [Request Sethttpbody:postdata]; [Request Settimeoutinterval:10.0];//GCD synchronizing asynchronous interface communication with UI updatesDispatch_async(Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{nsoperationqueue *queue = [[Nsoperationqueue alloc]init]; [nsurlconnectionSendasynchronousrequest:request queue:queue completionhandler:^ (nsurlresponse *response, NSData *data,Nserror*error) {if(Error) {NSLog(@"httperror:%@%d", error. Localizeddescription, error. Code); }Else{NsintegerResponsecode = [(Nshttpurlresponse *) response StatusCode];NSString*responsestring = [[NSStringAlloc] Initwithdata:data encoding:nsutf8stringencoding];NSLog(@"httpresponsecode:%d", Responsecode);NSLog(@"Httpresponsebody%@", responsestring); NSData *resdata = [[NSData alloc] initwithdata:[responsestring datausingencoding:nsutf8stringencoding]];nsdictionary*resultdic = [nsjsonserialization jsonobjectwithdata:resdata options:nsjsonreadingmutableleaves Error:Nil];NSLog(@"resultdic=%@", resultdic); arr = [[Resultdic valueforkey:@"pollutants"] valueforkey:@"Value"]; str = [[arr objectatindex:0] Objectatindex:0];NSLog(@"pm2.5=%@", [[Arr Objectatindex:0] Objectatindex:0]); } }];Dispatch_async(Dispatch_get_main_queue (), ^{ Self. Msgtext. Text= str. StringValue; }); });} - (void) Viewdidload {[SuperViewdidload];additional setup after loading the view, typically from a nib.[ SelfLoadData];//Start Reloaddata}- (ibaction) Reloaddata: (ID) Sender {NSLog(@"button Clicked"); [ SelfLoadData];NSLog(@"button Clicked");} - (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}
Please refer to the following links for details:
Source Address
iOS version PM2.5 air quality Monitor