【问题标题】:Getting user location when app is in background. IOS当应用程序在后台时获取用户位置。 IOS
【发布时间】:2015-09-05 13:45:08
【问题描述】:

我正在开发一个在后台运行的应用程序,以获取用户的位置并使用 http 请求将其发送到服务器。我的第一个意图是每 n 分钟获取一次用户的位置,但经过大量研究和试验,我放弃了,因为 ios 在 3 分钟后杀死了我的后台任务。

然后我尝试使用 MonitoringSignificantLocationChanges,但由于使用手机信号塔而导致其位置更新不准确,这破坏了我的应用程序的用途。

非常感谢以下任何一种解决方案:

  1. 每 n 分钟无限次在后台获取用户的位置。
  2. 在重要位置更改的后台以高精度获取用户的位置(使用 gps)
  3. 任何其他具有高精度结果的背景解决方案。

【问题讨论】:

    标签: ios core-location


    【解决方案1】:

    这对我有用,我使用 CLLocationManagerDelegate,在 didUpdateLocations 和 app Delegate 中注册更新

    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [_locationManager stopMonitoringSignificantLocationChanges];
        [_locationManager startUpdatingLocation];
    }
    

    我开始更新位置,对我来说,关键是当应用程序进入后台时,我切换到显着位置更改,这样应用程序就不会像这样消耗面糊:

    - (void)applicationDidEnterBackground:(UIApplication *)application {
        [_locationManager startMonitoringSignificantLocationChanges];
    }
    

    在didUpdateLocations中可以查看

    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;
    }
    

    并在后台启动一个任务来报告位置例如

    if (isInBackground) {
        [self sendBackgroundLocationToServer:self.location];
    }
    

    然后开始一个任务,希望对你有帮助。

    【讨论】:

      【解决方案2】:

      在重要位置更改的后台以高精度获取用户的位置(使用 gps)

      执行以下操作:

      info.plist中添加以下内容

          <key>NSLocationAlwaysUsageDescription</key>
          <string>{your app name} requests your location coordinates.</string>
          <key>UIBackgroundModes</key>
          <array>
              <string>location</string>
          </array>
      

      在代码中使用 LoctionManager 获取位置更新,(它将在前台和后台工作)

      @interface MyViewController <CLLocationManagerDelegate>
      @property (nonatomic, strong) CLLocationManager *locationManager;
      @end 
      
      @implementation MyViewController
      -(void)startLocationUpdates {
          // Create the location manager if this object does not
          // already have one.
          if (self.locationManager == nil) {
              self.locationManager = [[CLLocationManager alloc] init];
          }
      
          self.locationManager.delegate = self;
          self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
          self.locationManager.activityType = CLActivityTypeFitness;
      
          // Movement threshold for new events.
          self.locationManager.distanceFilter = 25; // meters
      
          if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
              [self.locationManager requestAlwaysAuthorization];
          }
          [self.locationManager startUpdatingLocation];
      }
      
      - (void)stopLocationUpdates {
          [self.locationManager stopUpdatingLocation];
      }
      
      #pragma mark CLLocationManagerDelegate
      
      - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
          // Add your logic here
      }
      - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
          NSLog(@"%@", error);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多