gpt4 book ai didi

ios - 区域监控当前位置不在退出时通知

转载 作者:技术小花猫 更新时间:2023-10-29 10:36:36 29 4
gpt4 key购买 nike

我正在尝试测试区域监控,因为我正在获取这样的当前位置:

- (void)startLocationTracking
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];

// Start location manager
if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
locationManager = [LocationTracker sharedLocationManager];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
}

然后像这样使用区域监控来跟踪第一个位置:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:manager.location.coordinate radius:300 identifier:@"first location initializer"];

NSLog(@"0: %@", manager.location);
NSLog(@"1: %@", region);

[manager startMonitoringForRegion:region];
NSLog(@"[locationManager startMonitoringForRegion:%@];", region);
});
}

然后在当前区域的每个导出中,我都会像这样监视新位置:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"%s, %@", __PRETTY_FUNCTION__, region);
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"%s, %@", __PRETTY_FUNCTION__, region);

NSArray *allRegions = manager.monitoredRegions.allObjects;

if (allRegions.count > 0) {
for (CLRegion *reg in allRegions) {
[manager stopMonitoringForRegion:reg];
}
}

CLLocationCoordinate2D cord = CLLocationCoordinate2DMake(manager.location.coordinate.latitude, manager.location.coordinate.longitude);
CLRegion *regionNew = [[CLRegion alloc] initCircularRegionWithCenter:cord radius:300 identifier:@"new region"];

NSLog(@"region: %@", region);
NSLog(@"regionNew: %@", regionNew);

[manager startMonitoringForRegion:regionNew];
}

我将解释我期望发生的事情:

  1. 在区域监控中注册当前位置。
  2. 通知用户退出当前区域。
  3. 退出日志并再次将当前位置注册为区域。

这不会发生。

我哪里错了?

我在模拟器上试过“Freeway Drive”。

更新:

经过测试和工作,由于地理围栏中的 Apple 错误,应用程序将仅支持 7.1+,非常糟糕,但我没有其他想法。

最佳答案

我认为您实现区域监控的方式可能会导致一些问题。

原因如下:-

  1. startLocationTracking 方法中,您的 locationManager 是一个本地对象,不会超出该方法的生命周期。这也意味着每次您调用 startLocationTracking 时,都会有一个新的 locationManager 对象分配有新的内存块。

    要解决此问题:您应该使用单例 locationManager,它是应用程序整个生命周期共享的locationManager

  2. 我认为您不应该在委托(delegate)方法 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:startMonitoringForRegion。原因是,如果您多次调用startLocationTracking,就会有多个locationManager。多个 locationManager 可以监控同一个区域,这可能会导致多个通知。

  3. 在您调用[manager startMonitoringForRegion:region];后,该区域不会立即被监控。如果你不相信相信我,试试下面的代码:-

    [locationManager startMonitoringForRegion:region];
    NSLog(@"%@",locationManager.monitoredRegions);

您会发现您刚刚监控的区域不会在locationManager.monitoredRegions 内。由于这是在 iOS 级别处理的,因此,我认为该区域可能需要几分钟时间才能准备好接受监控。

您还应该了解 iOS 中的区域监控的其他限制:-

https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

An app can register up to 20 regions at a time. In order to report region changes in a timely manner, the region monitoring service requires network connectivity.

In iOS 6, regions with a radius between 1 and 400 meters work better on iPhone 4S or later devices. (In iOS 5, regions with a radius between 1 and 150 meters work better on iPhone 4S and later devices.) On these devices, an app can expect to receive the appropriate region entered or region exited notification within 3 to 5 minutes on average, if not sooner.

Note: Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

我不知道你的应用是关于什么的,我相信你应该重新设计你的应用的流程。您应该尝试监视委托(delegate)方法之外的区域。

有关Singleton LocationManager的更多信息,您可以查看此答案:Background Location Services not working in iOS 7 . GitHub 上有一个完整的项目,其中包含一个Singleton LocationManager 类,我将其命名为LocationTracker

您可能还想查看我一个月前发现的 iOS 7 中的区域监控故障(有解决故障的变通方法):Region Monitoring Glitch on iOS 7 - Multiple Notifications at the same time

关于ios - 区域监控当前位置不在退出时通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24165599/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com