先放出demon地址:GitHub - AkaShark/-WeatherView: 待完善。
学习了获取手机位置,根据当前的位置获取当前的天气利用了AFN和知心天气APi.
首先在plist表正设置相关的权限
1.App Transport Security Settings 字典类型 ,Allow Arbitrary Loads bool类型 设置YES 为了可以请求网络
2.Privacy - Location When In Use Usage Description String类型 获取定位权限
获取定位
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>
创建CLLocationManager
初始化CLLocationManager
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate=self;//设置代理
// 定位频率,每隔多少米定位一次
// 距离过滤器,移动了几米之后,才会触发定位的代理函数
self.locationManager.distanceFilter = 100;
// 定位的精度,越精确,耗电量越高
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//导航
//请求允许在前台获取用户位置的授权
[self.locationManager requestWhenInUseAuthorization];
//求允许在前后台都能获取用户位置的授权
// [self.locationManager requestAlwaysAuthorization ];
//允许后台定位更新,进入后台后有蓝条闪动 (ios8)
// self.locationManager.allowsBackgroundLocationUpdates = YES;
//判断定位设备是否能用和能否获得导航数据
if ([CLLocationManager locationServicesEnabled]&&[CLLocationManager headingAvailable]){
[self.locationManager startUpdatingLocation];//开启定位服务
// [self.locationManager startUpdatingHeading];//开始获得航向数据
// 这个方法已被执行,就会回调下面的方法(代理方法)
// -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
}
else{
// NSLog(@"不能获得航向数据");//设置不能获取定位服务时的默认位置
[self sendRequestToServer:@"张家口"];
}
成功获取位置后的代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//locations 为一个数组 可以打印看下 里面包含了当前的位置信息 最后一个信息为当前的经度和纬度
self.currLocation = [locations lastObject];
//获取当前的海拔
self.altitudeStr = [NSString stringWithFormat:@"%3.2f",_currLocation.altitude];
//基于CLGeocoder - 反地理编码 (获取当前的位置信息 国家 城市 街道)
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:self.currLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0) {
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary = placemark.addressDictionary;
NSString *street = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
street = street == nil ? @"": street;
NSString *country = placemark.country;
NSString * subLocality = placemark.subLocality;
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
city = city == nil ? @"": city;
//发送请求
[self sendRequestToServer:city];
NSLog(@"%@",[NSString stringWithFormat:@"%@ \n%@ \n%@ %@ ",country, city,subLocality ,street]);
}
}];
}
发送请求
//发送请求 AFN
- (void)sendRequestToServer:(NSString *)cityName {
NSLog(@"%@",cityName);
_manager = [AFHTTPSessionManager manager];
NSString *url = [NSString stringWithFormat:@"https://api.thinkpage.cn/v3/weather/daily.json?key=osoydf7ademn8ybv&location=%@&language=zh-Hans&start=0&days=3",cityName];
//处理url请求中的中文字符
url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[_manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"response=%@",responseObject);
NSArray *resultArray = responseObject[@"results"];
for (NSDictionary *dic in resultArray) {
NSInteger type = [[dic[@"daily"] objectAtIndex:0][@"code_day"] integerValue];
//初始化model 将信息存入model中
_weatherModel = [[WeatherModel alloc]init];
_weatherModel.cityName = dic[@"location"][@"name"];
_weatherModel.todayDic = (NSDictionary *)[dic[@"daily"] objectAtIndex:0];
_weatherModel.tomorrowDic = (NSDictionary *)[dic[@"daily"] objectAtIndex:1];
_weatherModel.afterTomorrowDic = (NSDictionary *)[dic[@"daily"] objectAtIndex:2];
_weatherModel.altitudeStr = self.altitudeStr;
_weatherModel.weather = [self justTheWeather:type];
//将model传入View中 利用model的set方法更新UI
self.weatherView.model = _weatherModel;
self.weatherView.TVDelegate = self;
[self addSubview:self.weatherView];
//执行动画
[self addAnimationWithType:[dic[@"daily"] objectAtIndex:0][@"code_day"]];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
比较简单 写的也比较混乱 感觉自己写着写着就开始混乱了 希望大佬们帮忙看下 给点意见和指导!!
希望大家多交流~
QQ:1548742234