天气预报接口

27 篇文章 0 订阅
15 篇文章 0 订阅

ios关于天气预报接口

第三方“聚合数据”,天气预报每天免费调用100次,需要申请,需要实名认证。
在获取城市名称后,可根据城市名称获取该城市的天气信息。

currentCity设置为全局

NSString *url = [NSString stringWithFormat:@"https://op.juhe.cn/onebox/weather/query?cityname=%@&key=你申请的key",currentCity];
//NSLog(url);
NSString *str = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url1 = [NSURL URLWithString:str];
NSURLRequest *urlR = [NSURLRequest requestWithURL:url1];
NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlR completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSString *rec = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:[rec dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
    // NSLog(@"weather dic is %@", dic);
    NSDictionary *resultDic = [dic objectForKey:@"result"];
   // NSLog(@"resultArr is %@",resultDic);
    NSDictionary *dataDic = [resultDic objectForKey:@"data"];
    //NSLog(@"dataArr is %@",dataDic);
    NSArray *weatherArr = [dataDic objectForKey:@"weather"];
    //NSLog(@"weather is %@", weatherDic);
    NSArray *realtimeWeather = [dataDic objectForKey:@"realtime"];
    NSLog(@"realtimeWeather is %@",realtimeWeather);
    //回到主线程刷新页面
    dispatch_async(dispatch_get_main_queue(), ^{  
        //NSLog(@"weatherDic is %@");
        NSLog(@"-------------------------------");
        NSLog(@"weatherArr[0] is %@",weatherArr[0]);
        NSLog(@"地点:%@",currentCity);
        NSString *date = [weatherArr[0] objectForKey:@"date"]; //日期
        NSLog(@"日期:%@",date);
        NSDictionary *info = [weatherArr[0] objectForKey:@"info"];
        NSArray *day = [info objectForKey:@"day"];
        NSArray *night = [info objectForKey:@"night"];
        NSLog(@"温度%@-%@",night[2],day[2]);
        NSLog(@"天气:%@",day[1]);
        NSLog(@"风向风力:%@\%@",day[3],day[4]);
        NSString *week = [weatherArr[0] objectForKey:@"week"];
        NSLog(@"星期: %@",week);
    });
}];
[dataTask resume];

//NSLog(@"rgc = %@",[location.rgcData description]);//详细信息
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值