2015-05-20 92 views
1

我正在开发iOS应用程序,我想在每次启动应用程序时加载用户的当前位置。在每次发布应用程序时获取当前位置ios

我已经在didFinishLaunchingWithOptions中编写了这段代码,但是当我第一次启动我的应用程序时,它仅提取一次用户位置。 (我测试我的应用程序在iOS版5秒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

我认为你正在寻找applicationWillEnterForegroundapplicationDidBecomeActive,每次用户打开你的应用程序时都会调用它('变为活动状态')。

didFinishLaunchingWithOptions仅在应用程序初次启动时调用一次。

From the docs,状态转换事件的概述:

推出时间:

application:willFinishLaunchingWithOptions

application:didFinishLaunchingWithOptions

过渡到前台:

applicationDidBecomeActive

过渡到背景:

applicationDidEnterBackground

转变到不活动状态

applicationWillResignActive

applicationWillEnterForeground(转出的时调用(离开前台状态时调用。)背景状态)。

Ter mination:

applicationWillTerminate(仅在应用程序运行时调用。如果应用程序被暂停此方法不叫。)

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; 
    } 
} 
+0

该代码假定位置修复立即可用,这是不正确的。获得修复需要时间,这(假设手机信号塔连接可用)通常在3..10秒范围内。读取你最先定位的第一个位置读数很可能是无线电/蜂窝塔三角测量的结果,水平精度超过1000米。正确的方法是保持标准更新运行,直到获得具有可接受的水平精度的非陈旧(见时间戳!)读数。获得良好的阅读并不总是可能的,所以你应该考虑超时。 –

相关问题