加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 为什么我的CLLocationManager没有响应startMonitoringFor

发布时间:2020-12-14 17:27:44 所属栏目:百科 来源:网络整理
导读:在底部(在AppDelegate.m中),您可以看到我的CLLocationManager委托方法,它们都不会被调用.我正在使用GPX文件,但即使没有输入或退出区域,也应该调用委托方法didStartMonitoringForRegion. SomeOtherClass.m AppDelegate appDelegate = (AppDelegate *)[[UIAppl
在底部(在AppDelegate.m中),您可以看到我的CLLocationManager委托方法,它们都不会被调用.我正在使用GPX文件,但即使没有输入或退出区域,也应该调用委托方法didStartMonitoringForRegion.

SomeOtherClass.m

AppDelegate appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.locationManager startMonitoringForRegion:regionToMonitor desiredAccuracy:kCLLocationAccuracyNearestTenMeters];

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>
@property (nonatomic,retain) CLLocationManager *locationManager;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (locationManager==nil) {
        locationManager = [[CLLocationManager alloc] init];  
        locationManager.delegate = self;
    }

    return YES;
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"Did enter region");
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"Did exit region");
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Fail");
    NSLog(@"%@",[error description]);
}

-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    NSLog(@"Did start monitoring for region: %@",region.identifier);
}

解决方法

首先,您应该添加以下位置管理器委托方法,并查看由于某种原因区域监视注册失败:

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
    NSLog(@"%@",error);
}

其次,区域监控是系统共享资源.
该文档指出,它允许您监控有限数量的区域(不幸的是不指定任何数字),并提到如果另一个应用程序注册要监控的其他区域,您的某些应用程序监控区域可能会被丢弃.

第三,区域监测没有使用任何GPS技术.它仅使用网络操作符的蜂窝天线,无论何时更换蜂窝塔,它都会触发一个系统事件,该事件遍历所有受监控区域,并查看某个区域是否位于您现在所在的新区域内.

这意味着您应该期望服务的准确性降低,因此您应该增加为区域设置的半径.

最后,如果您的应用程序完全终止并且未被暂停,那么您的应用程序将在应用程序委托中接收:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

launchOptions字典中的UIApplicationLaunchOptionsLocationKey.
然后,您有责任在应用程序实时状态的有限时间内重新初始化您的位置管理器,获取当前位置并向用户发出本地推送通知.

由于应用程序不会在该阶段正常运行,但处于有限的后台模式状态.

此外,如果要在模拟器中测试应用程序,则应将track.gpx文件添加到项目中,并将模拟器设置为跟踪模式.确保放置在模拟器中的2个位置不是很远(因为完成跟踪可能需要很长时间)并将它们设置在路径中,它将进入受监控区域.然后看看你是否得到任何回调.不要使用您的设备进行测试,因为您需要实际走几个街区以查看与设备的任何真实交互:-)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读