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];
當我從燈塔走得足夠遠并走回范圍時,這種方法很有效.但是,如果我已經在信標區域得范圍內啟動應用程序,而不僅僅是當我回到邊界時,我還希望委托方法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全部內容,希望內容能夠幫你解決ios – iBeacon – 應用程序在區域啟動時未調用didEnterRegion所遇到得程序開發問題。
如果覺得來客網網站內容還不錯,歡迎將來客網網站推薦給程序員好友。