gpt4 book ai didi

ios - iBeacon - 当应用程序在区域中启动时没有调用 didEnterRegion

转载 作者:可可西里 更新时间:2023-11-01 04:43:59 26 4
gpt4 key购买 nike

我制作了一个注册信标区域并使用 CLLocationManager 开始监控这些区域的应用

CLLocationManager *manager = [[CLLocationManager alloc] init];
manager.delegate = self;

CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID major:12445 identifier:@"id"];
region.notifyEntryStateOnDisplay = YES;
region.notifyOnEntry = YES;
[manager startMonitoringForRegion:region];

当我从信标走得足够远并回到范围内时,这非常有效。但是,如果我已经在信标区域范围内启动应用程序,我也希望委托(delegate)方法 didEnterRegion 触发,而不仅仅是当我回到边界时。有没有一种简单的方法可以实现这一目标?或者让 CLLocationManager 认为我们已经离开信标范围的方法?

另一篇文章说,设置 region.notifyEntryStateOnDisplay = YES; 并按下保持按钮可以做到这一点 - 但我没有得到这个工作(iOS 7.1,iPhone 5S)。

最佳答案

来自苹果的文档:

Monitoring of a geographical region begins immediately after registration for authorized apps. However, don’t expect to receive an event right away, because only boundary crossings generate an event. In particular, if the user’s location is already inside the region at registration time, the location manager doesn’t automatically generate an event. Instead, your app must wait for the user to cross the region boundary before an event is generated and sent to the delegate. To check whether the user is already inside the boundary of a region, use the requestStateForRegion: method of the CLLocationManager class.

所以我最终这样做了:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSDictionary *regionDictionary;
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// setup regions in case you have multiple regions
self.regionDictionary = @{@"com.test" : @"2FAE2A83-1634-443B-8A0C-56704F81A181"};

// setup location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}

[self.locationManager startUpdatingLocation];

//start monitoring for all regions
for (NSString *key in self.regionDictionary.allKeys) {
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[key]] identifier:key];
[self.locationManager startMonitoringForRegion:beaconRegion];
}
}

- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region {
if (region.identifier.length != 0) {
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[region.identifier]] identifier:region.identifier];
[self.locationManager startRangingBeaconsInRegion:beaconRegion];
}
}

- (void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region {
if (region.identifier.length != 0) {
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[region.identifier]] identifier:region.identifier];
[self.locationManager stopRangingBeaconsInRegion:beaconRegion];
}
}

- (void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region {
// Beacon found!
CLBeacon *foundBeacon = [beacons firstObject];
NSLog(@"UUID:%@; major:%@; minor:%@;", foundBeacon.proximityUUID.UUIDString, foundBeacon.major, foundBeacon.minor);
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
if ([region isKindOfClass:[CLBeaconRegion class]] && state == CLRegionStateInside) {
[self locationManager:manager didEnterRegion:region];
}
}

- (void)locationManager:(CLLocationManager *) manager didStartMonitoringForRegion:(CLRegion *) region {
[manager requestStateForRegion:region];
}

关于ios - iBeacon - 当应用程序在区域中启动时没有调用 didEnterRegion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133494/

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