iOS版本PM2.5空氣品質監控儀

來源:互聯網
上載者:User

iOS版本PM2.5空氣品質監控儀
前言

鑒於柴靜版《蒼穹之下》的火爆,或許更多的人會關心環境,空氣品質等環保因素,參考某學院的Swift版本的PM2.5的擷取,本人自己寫個Objective-C版本的。

基本原理

從伺服器擷取當前的空氣品質資料,氣象中心開放介面, 亞洲空氣品質即時監控, 參照人家的教程使用亞洲空氣品質監控。
本地解析資料,擷取PM2.5數值,進行顯示。

具體實現
NSNumber *str;-(void) loadData{self.msgText.text = @loading....;NSURL *url = [NSURL URLWithString:@http://aqicn.org/publishingdata/json];NSString *post=@postData;NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];[request setHTTPMethod:@POST];[request setHTTPBody:postData];[request setTimeoutInterval:10.0];//GCD實現非同步介面通訊與UI更新的同步dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 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: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 {[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 didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.