【问题标题】:CLLocationManager didUpdateToLocation not being called after some time一段时间后没有调用 CLLocationManager didUpdateToLocation
【发布时间】:2013-01-24 14:02:52
【问题描述】:

我正在尝试随着时间的推移记录用户位置。如果用户在移动,它工作正常,并且委托方法 didUpdateToLocation 被可靠地调用。但是,如果用户静止并且应用程序在后台运行,那么一段时间后,不再调用委托方法。要重新启动它,需要将应用程序购买到前台。一旦它处于活动状态,委托方法就会再次可靠地调用。

我最初认为这可能是因为 CLLocationManager 对象是在 ViewController 中声明的,所以我将其更改为在 AppDelegate 中声明,但这也没有帮助。

我也尝试过 distanceFilter 属性,但无济于事。我目前正在使用 View 控制器中的以下代码进行设置。请注意,对象本身是在 AppDelegate 对象中声明和初始化的。

 app.locationManager.delegate = self;
    app.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    app.locationManager.distanceFilter = kCLDistanceFilterNone;
    [app.locationManager startUpdatingLocation];

还有其他人遇到过这个问题吗?任何指针将不胜感激。我已经为此苦苦挣扎了几天。

【问题讨论】:

    标签: iphone ios cllocationmanager


    【解决方案1】:

    iOS 6 引入了 CLLocationManager 属性 pausesLocationUpdatesAutomatically。设置 CLLocationManager 时需要将其设置为 NO,如下所述:http://www.stackoverflow.com/a/12781634/700769

    【讨论】:

    • 这听起来很有希望。我稍后会尝试并适当地更新帖子。
    【解决方案2】:

    在 UpdateLocation 方法中添加此代码

    - (void) updateLocation{
        self.locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.pausesLocationUpdatesAutomatically = NO;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
        if ([CLLocationManager locationServicesEnabled]) {
            [locationManager startUpdatingLocation];       
        } else {
            NSLog(@"Location services is not enabled");
        }
    }
    

    还在 Schemes 中编辑设置:Scheme/Edit Scheme/Options/Allow Location Simulation 已选中,但没有设置默认位置。

    【讨论】:

      【解决方案3】:

      您需要在应用 plist 文件的 UIBackgroundModes 中添加位置。

      【讨论】:

      • 同意。请参阅 iOS 应用程序编程指南中的 UIBackgroundModesApp States and Multitasking
      • 我应该更清楚。我也已经这样做了。它在后台运行良好,直到用户移动。一旦用户停止移动并在一个地方停留了一段时间,代理就会停止被调用。
      【解决方案4】:
      if #available(iOS 9.0, *){
          locationManager.allowsBackgroundLocationUpdates = true;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-23
        • 1970-01-01
        相关资源
        最近更新 更多