博客> iOS 获取当前天气情况
iOS 获取当前天气情况
2020-01-21 08:05 评论:0 阅读:939 Mr_chenxun
天气

根据抓取的天气状况api获取当前位置的当前天气状况,一个重要前提就是先地图定位当前城市,定位的方法这里就不说了,毕竟此贴主要是针对天气

pragma mark - 天气

  • (void)setWeather{ NSString httpUrl = @"http://api.map.baidu.com/telematics/v3/weather"; NSString lng = [Util userDefaultsDataForKey:@"longitude"]; NSString lat = [Util userDefaultsDataForKey:@"latitude"]; if ([lng isEqualToString:@""]&&[lat isEqualToString:@""]) { lng = @"121.505856"; lat = @"31.216019"; } NSString location = [NSString stringWithFormat:@"%@,%@",lng,lat]; NSString ak = @"6tYzTvGZSOpYB5Oc2YGGOKt8"; NSString httpArg = [NSString stringWithFormat:@"location=%@&output=json&ak=%@",location,ak]; [self request: httpUrl withHttpArg: httpArg];

}

//数据的请求

//传递经纬度参数 -(void)request: (NSString)httpUrl withHttpArg: (NSString)HttpArg { NSString urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg]; NSURL url = [NSURL URLWithString: urlStr]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; [request setHTTPMethod: @"GET"]; [request addValue: @"" forHTTPHeaderField: @"apikey"];

[NSURLConnection sendAsynchronousRequest: request
                                   queue: [NSOperationQueue mainQueue]
                       completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                           if (error) {
                               NSLog(@"Httperror: %@%ld", error.localizedDescription, (long)error.code);
                           } else {
                               NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                               NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               SBJson4Parser *jsonParser = [SBJson4Parser parserWithBlock:^(id item, BOOL *stop) {
                                   NSObject *itemObject = item;

                                   if ([item isKindOfClass:[NSDictionary class]]) {

                                       //接收获取下来的数据,然后获取数据进行相对应的赋值
                                       NSDictionary *dict = (NSDictionary *)itemObject;
                                       NSArray *dicArr = dict[@"results"];
                                       NSDictionary *weartherDic = nil;
                                       for (NSDictionary *dict in dicArr) {
                                           NSArray *weatherArr = dict[@"weather_data"];
                                           weartherDic = [weatherArr objectAtIndex:0];
                                       }

                                       NSString *temperature = weartherDic[@"temperature"];
                                       NSString *weatherName = weartherDic[@"weather"];
                                       NSArray *strArr = [temperature componentsSeparatedByString:@" ~ "];
                                       NSLog(@"+++++++=%@",strArr);
                                       //去掉摄氏度
                                       NSString *lowtem = nil;
                                       NSString *highttem = nil;
                                       NSArray *subStr = [strArr[1] componentsSeparatedByString:@"℃"];
                                       lowtem = subStr[0];
                                       highttem = strArr[0];
                                       [Util setUserDefaultsData:lowtem forKey:@"lowTmp"];
                                       [Util setUserDefaultsData:highttem forKey:@"hightTmp"];
                                       NSLog(@"subStr++++++=%@",lowtem);
                                       [Util setUserDefaultsData:temperature forKey:@"weather"];
                                       [Util setUserDefaultsData:[NSString stringWithFormat:@"%@",weatherName] forKey:@"weatherName"];
                                       //代理传递信息
                                       [self.delegates addWeatherView:self];

                                   }
                               } allowMultiRoot:NO unwrapRootArray:NO errorHandler:^(NSError *error) {
                                   NSLog(@"%@",error);
                               }];
                               [jsonParser parse:data];
                               NSLog(@"HttpResponseCode:%ld", (long)responseCode);

                               NSLog(@"HttpResponseBody %@",responseString);
                           }
                       }];

}

转自CSDN :http://blog.csdn.net/baidu_30511379/article/details/52450446

收藏
1
sina weixin mail 回到顶部