IOS version PM2.5 air quality monitor
Preface
In view of the popularity of Chai Jing's "under the sky", more people may be concerned about environmental, air quality and other environmental factors. Refer to the acquisition of PM2.5 from a Swift version of a college, write an Objective-C version by myself.
Basic Principles
Obtain the current air quality data from the server, open interfaces of the Meteorological Center, and real-time monitoring of air quality in Asia.
Parse the data locally and obtain the PM2.5 value for display.
Implementation
NSNumber * str;-(void) loadData {self. msgText. text = @ loading ....; NSURL * url = [NSURL URLWithString: @ brief * post = @ postData; NSData * postData = [post dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion: YES]; NSMutableURLRequest * request = [Your requestWithURL: url]; [request setHTTPMethod: @ POST]; [request setHTTPBody: postData]; [request setTimeoutInterval: 10.0]; // GCD implements synchronous dispatch_async (callback (success, 0), ^ {NSOperationQueue * queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * error) {if (error) {NSLog (@ Httperror: % @ % d, error. localizedDescription, error. code);} else {NSInteger responseCode = [(NSHTTPURLResponse *) response statusCode]; NSString * responseString = [[NSString alloc] initWithData: data encoding: callback]; NSLog (@ HttpResponseCode: % d, responseCode); NSLog (@ HttpResponseBody % @, responseString); NSData * resData = [[NSData alloc] initWithData: [responseString dataUsingEncoding: NSUTF8StringEncoding]; NSDictionary * resultDic = [NSJSONSerialization failed: resData options: Failed 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 {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self loadData]; // start reloadData}-(IBAction) reloadData :( id) sender {NSLog (@ Button Clicked); [self loadData]; NSLog (@ Button Clicked );} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .}