繁体   English   中英

locationManager didExitRegion或locationManager didEnterRegion从未调用

[英]locationManager didExitRegion or locationManager didEnterRegion never called

   - (void)viewDidLoad
{

    [super viewDidLoad];

     if([CLLocationManager locationServicesEnabled]){

        self.locationManager = [[CLLocationManager alloc] init];

        self.locationManager.delegate = self;

        CLLocationCoordinate2D companyCenter;

        companyCenter.latitude = 23.126272;

        companyCenter.longitude = 113.395568;


        CLRegion* fkit = [[CLCircularRegion alloc] initWithCenter:companyCenter
                                                           radius:500 identifier:@"fkit"];
        [self.locationManager startMonitoringForRegion:fkit];

    }else{
        NSLog(@"not support");
    }

}

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

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    NSLog(@"Region monitoring failed with error: %@", [error localizedDescription]);
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(nonnull CLRegion *)region
{
    NSLog(@"Entered Region - %@", region.identifier);
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(nonnull CLRegion *)region
{
    NSLog(@"Entered Enter Region - %@", region.identifier);
}

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

以上是我的代码。 我在没有位置的模拟器中运行,然后在“调试”->“位置”项中设置了自定义位置(23.126272,113.395568),并且它从未调用didEnterRegion委托。 有人可以帮助我吗? PS:我的Xcode是7.1.1,并且控制台日志“已开始监视fkit区域”

您缺少几个步骤。 您必须在info.plist中添加一个条目,声明您想在应用程序处于前台时使用位置服务(使用NSLocationWhenInUseUsageDescription键),然后需要检查是否具有权限并提出要求如果不这样做。

在Xcode文档中搜索字符串“请求使用位置服务的权限”以获取更多信息。

在didStartMonitoringForRegion中添加此行

[self.locationManager requestStateForRegion:yourregion];

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM