2015-05-20 113 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米。正確的方法是保持標準更新運行,直到獲得具有可接受的水平精度的非陳舊(見時間戳!)讀數。獲得良好的閱讀並不總是可能的,所以你應該考慮超時。 –

相關問題