简体   繁体   English

CLLocationManager仅在安装后第一次运行应用程序时监视区域

[英]CLLocationManager only monitors regions the first time the app is run after install

I have an iOS app which is a tabbed application, with 3 view controllers, all of which need to know when the phone enters specific geographical regions. 我有一个iOS应用程序,它是一个标签式应用程序,有3个视图控制器,所有这些都需要知道手机什么时候进入特定的地理区域。

The regions which we monitor for are supplied at run time over a web interface, so we need to periodically clear the regions that the CLLocationManager is monitoring for and add new ones. 我们监视的区域是在运行时通过Web界面提供的,因此我们需要定期清除CLLocationManager正在监视的区域并添加新区域。 The CLLocationManager object is a member variable of a singleton class which manages the connection with the web server as well. CLLocationManager对象是singleton类的成员变量,它也管理与Web服务器的连接。

The problem that I have is that when the application is first installed, the region monitoring works fine. 我遇到的问题是,首次安装应用程序时,区域监控工作正常。 But the first time I try to run it after that first time, the region monitoring does not work. 但是我第一次尝试在第一次运行它之后 ,区域监控不起作用。

I can see this on both the actual handset and the iOS simulator. 我可以在实际手机 iOS模拟器上看到这一点。

On receipt of the mesasge from the server which contains the region details, we run the following code: 收到包含区域详细信息的服务器的mesasge后,我们运行以下代码:

-(void) initialiseLocationManager:(NSArray*)geofences
{
    if(![CLLocationManager locationServicesEnabled])
    {
        NSLog(@"Error - Location services not enabled");
        return;
    }
    if(self.locationManager == nil)
    {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
    }
    else
    {
        [self.locationManager stopUpdatingLocation];
    }
    for(CLRegion *geofence in self.locationManager.monitoredRegions)
    {
        //Remove old geogate data
        [self.locationManager stopMonitoringForRegion:geofence];
    }
    NSLog(@"Number of regions after cleanup of old regions: %d", self.locationManager.monitoredRegions.count);
    if(self.locationManager == nil)
    {
        [NSException raise:@"Location manager not initialised" format:@"You must intitialise the location manager first."];
    }
    if(![CLLocationManager regionMonitoringAvailable])
    {
        NSLog(@"This application requires region monitoring features which are unavailable on this device");
        return;
    }
    for(CLRegion *geofence in geofences)
    {
        //Add new geogate data

        [self.locationManager startMonitoringForRegion:geofence];
        NSLog(@"Number of regions during addition of new regions: %d", self.locationManager.monitoredRegions.count);
    }
    NSLog(@"Number of regions at end of initialiseRegionMonitoring function: %d", self.locationManager.monitoredRegions.count);
    [locationManager startUpdatingLocation];
}

I have tried calling [locationmanager stopUpdatingLocation] in various places, in particular in various places in the AppDelegate.m file (applicationWilLResignActive, applicationDidEnterBackground, applicationWillTerminate), but none of them seem to help. 我曾尝试在各个地方调用[locationmanager stopUpdatingLocation],特别是在AppDelegate.m文件中的各个地方(applicationWilLResignActive,applicationDidEnterBackground,applicationWillTerminate),但它们似乎都没有帮助。 Either way, when I build my application and add a GPX file to simulate locations, the simulator correctly picks up the regions being sent by the web interface. 无论哪种方式,当我构建我的应用程序并添加GPX文件来模拟位置时,模拟器会正确地选取Web界面发送的区域。 The second time I run the program, the regions are not picked up. 第二次运行程序时,区域没有被选中。 When I reload the GPX file, it works again, but from the second time onwards, it doens't work any more. 当我重新加载GPX文件时,它再次起作用,但从第二次开始,它再也无法工作了。

According to the API documentation, the CLLocationManager keeps a record of regions even on termination (which is why I clear down the regions which we monitor for), but my guess is that my initialisation routine is good for the first time the app runs, but calls things that shouldn't be called from the second time onwards. 根据API文档,CLLocationManager甚至在终止时保留区域记录(这就是我清除我们监视的区域的原因),但我的猜测是我的初始化例程在应用程序第一次运行时是好的,但是调用第二次不应该调用的东西。 Also, the clearing down process doesn't always seem to work (the NSLog statement often shows the CLLocationManager clearing down to 0 regions, but not always). 此外,清除过程似乎并不总是有效(NSLog语句通常显示CLLocationManager清除为0个区域,但并非总是如此)。

Any ideas why this isn't working? 任何想法为什么这不起作用?

So lets clean this up a little bit 所以让我们清理一下吧

You dont need to call startUpdatingLocation and stopUpdatingLocation when using region monitoring. 使用区域监视时,您不需要调用startUpdatingLocationstopUpdatingLocation Those activate the standard location tracking sending messages to the delegate callback locationManager:didUpdateLocations: . 那些激活标准位置跟踪发送消息到委托回调locationManager:didUpdateLocations: . Region tracking apps implement these delegate callbacks: 区域跟踪应用程序实现这些委托回调:

– locationManager:didEnterRegion:
– locationManager:didExitRegion:

Also its very unlikely that location services will become disabled in the course of this method and you should make sure its not possible to get rid of the 'locationManager' from a background thread. 此方法也不太可能在此方法中禁用位置服务,您应确保无法从后台线程中删除“locationManager”。 Therefore we dont need to check them twice. 因此我们不需要检查它们两次。

As you are region monitoring its best to make sure you have region monitoring available with + (BOOL)regionMonitoringAvailable . 由于您在区域内监控其最佳状态,以确保您可以使用+ (BOOL)regionMonitoringAvailable进行区域监控。 Also best to check the location permissions with + (CLAuthorizationStatus)authorizationStatus at some point and react appropriately. 最好还是在某个时刻使用+ (CLAuthorizationStatus)authorizationStatus检查位置权限并做出适当的反应。

As per this blog post it seems you also need to have 根据这篇博客文章 ,你似乎也需要

UIBackgroundModes :{location}
UIRequiredDeviceCapabilities: {location-services}

in your apps info.plist for this all to work correctly. 在您的应用程序info.plist中,这一切都能正常工作。

If you have more information about the mode of failure i can come back with some more specific advice :) 如果您有关于失败模式的更多信息,我可以回来一些更具体的建议:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅在首次运行应用程序时显示键盘时崩溃 - Crash while showing the keyboard only the first time app run 一段时间后没有调用CLLocationManager didUpdateToLocation - CLLocationManager didUpdateToLocation not being called after some time 重建应用程序后,iOS CLLocationManager无法接收更新 - iOS CLLocationManager not receiving updates after rebuilding app iPhone应用程序在首次运行/安装(apns?)时崩溃,但是在…附加日志后还可以 - iPhone app crashes on first run/install (apns?) but ok after… logs attached 我的iPhone核心数据应用程序首次完美运行,但之后运行时崩溃 - My iPhone core data app runs perfectly the first time but crashes when run after that 有什么方法可以知道应用程序是否第一次运行 - is there any method to know if the app run for the first time iPhone应用程序只能在模拟器上首次使用 - iphone app only works first time on simulator CoreLocation:一次仅监视某些区域 - CoreLocation: monitor only certain regions at a time 仅在首次启动应用程序时显示UIAlertView - Showing an UIAlertView the first time the app is launched only 创建ViewController仅在应用程序首次运行iOS时运行 - Creating ViewController to only run first time the application is run iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM