【问题标题】:iOS App doesn't recieve location updates when in backgroundiOS 应用在后台时不接收位置更新
【发布时间】:2016-02-18 13:51:14
【问题描述】:

我的应用在使用和后台运行时需要频繁更新位置。

我已启用后台模式 --> 我的应用程序的位置更新,但我的应用程序在进入后台模式约 10 秒后停止接收位置更新。

我的代码:

func initializeLocationManager()
{
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.distanceFilter = 1
}

func startLocationManager()
{
    locationManager.startUpdatingLocation()
}

我还在 info.plist 中添加了以下内容:

NSLocationAlwaysUsageDescription

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 关注answer 可能会对您有所帮助。

标签: ios swift location


【解决方案1】:

这解决了我的问题:

    if #available(iOS 9.0, *) {
        locationManager.allowsBackgroundLocationUpdates = true
    } else {
        // Fallback on earlier versions
    }

【讨论】:

    【解决方案2】:

    要获得一致的更新,您可能需要每 5 分钟执行一次操作。操作方法在herehere 中进行了描述。

    这是来自 Ricky here 的解决方案:-

    如果您的应用是基于位置的移动应用,需要在设备发生重大变化时监控设备的位置,则当设备从最后一个已知位置移动超过 500 米时,iOS 会返回一些位置坐标。是的,即使应用程序被用户或 iOS 本身杀死/暂停,您仍然可以获得位置更新。

    所以为了让locationManager即使在应用被终止/挂起时也能获得位置更新,你必须使用startMonitoringSignificantLocationChanges的方法,你不能使用startUpdatingLocation

    当iOS想要将位置更新返回给应用程序时,它会帮助你重新启动应用程序并将密钥UIApplicationLaunchOptionsLocationKey返回给应用程序委托方法didFinishLaunchingWithOptions

    密钥UIApplicationLaunchOptionsLocationKey 非常重要,你必须知道如何处理它。您必须在收到密钥时创建一个新的 locationManager 实例,您将在 locationManager 委托方法didUpdateLocations 上获得位置更新。

    这里是示例代码:-

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
        self.shareModel = [LocationShareModel sharedModel];
    
        if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) { 
          self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
          self.shareModel.anotherLocationManager.delegate = self;
          self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
          self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
    
          if(IS_OS_8_OR_LATER) {
            [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
          }
    
         [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];   
    
        }    
            return YES;
     }
    

    除了didFinishLaunchingWithOptions 方法之外,我还在应用程序处于活动状态时创建了 locationManager 实例。以下是一些代码示例:

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];
    
        if(IS_OS_8_OR_LATER) {
            [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
        }
    
        [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        if(self.shareModel.anotherLocationManager)
            [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];
    
        self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
        self.shareModel.anotherLocationManager.delegate = self;
        self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
    
        if(IS_OS_8_OR_LATER) {
            [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
        }
    
        [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
    }
    

    我写了一篇文章,详细解释了如何在 iOS 7 和 8 中获取位置更新,即使应用程序被终止/挂起。我还在 GitHub 上上传了完整的源代码以及如何测试此解决方案的步骤。

    请访问以下网址了解更多信息:-

    1. Getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended

    2. Source Code on GitHub - Get the Location Updates Even when the iOS mobile apps is Suspended/Terminated/Killed

    【讨论】:

    • 感谢您的反馈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多