先放出demon地址:GitHub - AkaShark/-WeatherView: 待完善。
學(xué)習(xí)了獲取手機位置,根據(jù)當前的位置獲取當前的天氣利用了AFN和知心天氣APi.
首先在plist表正設(shè)置相關(guān)的權(quán)限
1.App Transport Security Settings 字典類型 ,Allow Arbitrary Loads bool類型 設(shè)置YES 為了可以請求網(wǎng)絡(luò)
2.Privacy - Location When In Use Usage Description String類型 獲取定位權(quán)限
獲取定位?
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>
創(chuàng)建CLLocationManager
初始化CLLocationManager
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate=self;//設(shè)置代理
//? 定位頻率,每隔多少米定位一次
// 距離過濾器,移動了幾米之后,才會觸發(fā)定位的代理函數(shù)
self.locationManager.distanceFilter = 100;
// 定位的精度,越精確,耗電量越高
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//導(dǎo)航
//請求允許在前臺獲取用戶位置的授權(quán)
[self.locationManager requestWhenInUseAuthorization];
//求允許在前后臺都能獲取用戶位置的授權(quán)
//? ? [self.locationManager requestAlwaysAuthorization ];
//允許后臺定位更新,進入后臺后有藍條閃動 (ios8)
//? ? self.locationManager.allowsBackgroundLocationUpdates = YES;
//判斷定位設(shè)備是否能用和能否獲得導(dǎo)航數(shù)據(jù)
if ([CLLocationManager locationServicesEnabled]&&[CLLocationManager headingAvailable]){
[self.locationManager startUpdatingLocation];//開啟定位服務(wù)
//? ? ? ? [self.locationManager startUpdatingHeading];//開始獲得航向數(shù)據(jù)
//? ? ? ? 這個方法已被執(zhí)行,就會回調(diào)下面的方法(代理方法)
//? ? ? ? -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
}
else{
//? ? ? ? NSLog(@"不能獲得航向數(shù)據(jù)");//設(shè)置不能獲取定位服務(wù)時的默認位置
[self sendRequestToServer:@"張家口"];
}
成功獲取位置后的代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//locations 為一個數(shù)組 可以打印看下 里面包含了當前的位置信息 最后一個信息為當前的經(jīng)度和緯度
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;
//發(fā)送請求
[self sendRequestToServer:city];
NSLog(@"%@",[NSString stringWithFormat:@"%@ \n%@ \n%@? %@ ",country, city,subLocality ,street]);
}
}];
}
發(fā)送請求
//發(fā)送請求 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];
//執(zhí)行動畫
[self addAnimationWithType:[dic[@"daily"] objectAtIndex:0][@"code_day"]];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"請求失敗");
}];
}
比較簡單 寫的也比較混亂 感覺自己寫著寫著就開始混亂了 希望大佬們幫忙看下 給點意見和指導(dǎo)??!
希望大家多交流~
QQ:1548742234