iOS-定位总结
info.plist 请求用户位置授权
1 2 3 4
| <key>NSLocationWhenInUseUsageDescription</key> <string>需要使用位置</string> <key>NSLocationAlwaysUsageDescription</key> <string>需要使用位置</string>
|
代码
language: 要生成的位置信息的语言(’China‘还是’中国‘)
- 中文 : @”zh-hans”
- 英文 : @”en”
- 日文 : @”jp”
1
| - (void)locationWithLanguage:(NSString *)language location:(void(^)(NSString *country,NSString *province))location;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| @interface DKLocation()<CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager *locationManager;
@end @implementation DKLocation - (void)locationWithLanguage:(NSString *)language location:(void(^)(NSString *country,NSString *province))location { [[self rac_signalForSelector:@selector(locationManager:didUpdateToLocation:fromLocation:) fromProtocol:@protocol(CLLocationManagerDelegate)] subscribeNext:^(RACTuple *tuple) { CLLocationManager *manager = tuple.first; CLLocation *newLocation = tuple.second; [manager stopUpdatingLocation]; [_locationManager stopUpdatingLocation]; _locationManager.delegate = nil; NSString *userDefaultLanguages = DKUserDefaults(kAppLanguage); [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:language,nil] forKey:@"AppleLanguages"]; CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { if(!error){ for (CLPlacemark * placemark in placemarks) { NSString *provinceName = placemark.administrativeArea; NSString *country = placemark.country; location(country,provinceName); DKLog(@"%@%@",country,provinceName); break; } } [[NSUserDefaults standardUserDefaults] setObject:@[userDefaultLanguages] forKey:@"AppleLanguages"]; }];
}]; _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.distanceFilter = kCLDistanceFilterNone; if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager requestAlwaysAuthorization]; } [_locationManager startUpdatingLocation]; }
@end
|