【发布时间】:2015-08-01 04:22:12
【问题描述】:
我正在开发 iOS 应用,我想在每次启动应用时加载用户的当前位置。
我已经在didFinishLaunchingWithOptions 中编写了这段代码 sn-p,但是当我第一次启动我的应用程序时,它只获取用户位置一次。 (我正在 5s iOS 7 中测试我的应用)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if(IS_OS_8_OR_LATER){
NSUInteger code = [CLLocationManager authorizationStatus];
if (code == kCLAuthorizationStatusNotDetermined && ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
// choose one request according to your business.
if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[locationManager requestAlwaysAuthorization];
} else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
[locationManager requestWhenInUseAuthorization];
} else {
NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
}
}
}
[locationManager startUpdatingLocation];
...
...
...
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
[locationManager stopUpdatingLocation];
}
现在,当我回家时,位置已更改,当我尝试打开我的应用程序时,它不会获取家庭位置,而是仅显示我的旧位置。(我的旧位置和家庭位置之间存在很大差异)
请帮忙,提前致谢。
【问题讨论】:
标签: ios cllocationmanager cllocation