iOS version PM2.5 air quality Monitor

Source: Internet
Author: User

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

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.