【问题标题】:Fetch current location on each launch of app ios在每次启动应用程序 ios 时获取当前位置
【发布时间】: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


    【解决方案1】:

    我认为您正在寻找 applicationWillEnterForeground 或 applicationDidBecomeActive,每次用户打开您的应用程序(“变为活动状态”)时都会调用它们。

    didFinishLaunchingWithOptions 仅在您的应用程序首次启动时被调用一次。

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

    发射时间:

    application:willFinishLaunchingWithOptions

    application:didFinishLaunchingWithOptions

    过渡到前台:

    applicationDidBecomeActive

    过渡到后台:

    applicationDidEnterBackground

    过渡到非活动状态

    applicationWillResignActive(离开前台状态时调用。)

    applicationWillEnterForeground(退出后台状态时调用。)

    终止:

    applicationWillTerminate(仅在应用运行时调用,应用挂起时不调用此方法。)

    【讨论】:

      【解决方案2】:

      在 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;
          }
      }
      

      【讨论】:

      • 代码假定位置修复立即可用,这是不正确的。获得修复需要时间,(假设手机信号塔连接可用)通常在 3..10 秒范围内。您要确定的第一个位置读数很可能是 wifi/蜂窝塔三角测量的结果,水平精度超过 1,000 米。正确的方法是保持标准更新运行,直到获得具有可接受水平精度的非陈旧(参见时间戳!)读数。获得良好的阅读并不总是可能的,因此您也应该考虑超时。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多