我正在开发iOS应用程序,我想在每次启动app时加载用户的当前位置。
我已在didFinishLaunchingWithOptions
中编写了此代码段,但它仅在我启动应用时第一次获取用户位置。 (我在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];
}
现在,当我回家时,位置发生变化,当我尝试打开我的应用程序时,它不会取回家的位置,而只显示我的旧位置。(我的旧位置与家庭之间存在很大差异位置)
请提供帮助,并提前致谢。
答案 0 :(得分:0)
我认为您正在寻找applicationWillEnterForeground
或applicationDidBecomeActive
,每次用户打开您的应用程序时都会调用它们('变为活动状态')。
didFinishLaunchingWithOptions
仅在您的应用程序首次启动时调用一次。
From the docs,概述了州过渡事件:
发布时间:
application:willFinishLaunchingWithOptions
application:didFinishLaunchingWithOptions
转换到前台:
applicationDidBecomeActive
过渡到后台:
applicationDidEnterBackground
转换为非活动状态
applicationWillResignActive
(离开前景状态时调用。)
applicationWillEnterForeground
(在退出背景状态时调用。)终止:
applicationWillTerminate
(仅在应用程序运行时调用。如果应用程序暂停,则不会调用此方法。)
答案 1 :(得分:0)
在 applicationDidBecomeActive 中编写代码。每次来自背景时都会调用它。
- (void)applicationDidBecomeActive:(UIApplication *)application {
//Get Current Location
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
}
else
{
NSLog(@"Location services are not enabled");
}
}
#pragma mark -- Location Delegate Methods
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
if (location != nil)
NSLog(@"current lat:%f , currentLong:%f",location.coordinate.latitude,location.coordinate.longitude);
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Error = %@",error.localizedDescription);
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied:
{
// do some error handling
}
break;
default:{
[self.locationManager startUpdatingLocation];
}
break;
}
}