如果我在該區域內啓動應用程序,我的應用程序不會觸發didEnterRegion事件。如果我在該區域外啓動應用程序,然後進入該區域,則會觸發。如果我在該區域內啓動該應用程序,則離開該區域,然後重新進入該區域,該區域會被觸發。如果在區域內啓動應用程序,startMonitoringForRegion不會調用didEnterRegion
有關如何在應用程序打開後立即啓動它的任何建議,如果它在該地區將非常感激!
如果我在該區域內啓動應用程序,我的應用程序不會觸發didEnterRegion事件。如果我在該區域外啓動應用程序,然後進入該區域,則會觸發。如果我在該區域內啓動該應用程序,則離開該區域,然後重新進入該區域,該區域會被觸發。如果在區域內啓動應用程序,startMonitoringForRegion不會調用didEnterRegion
有關如何在應用程序打開後立即啓動它的任何建議,如果它在該地區將非常感激!
我不認爲你可以這樣做。
但是,您可以獲取當前位置並檢查它是否在您指定的區域內。 CLCircularRegion
對此有一個containsCoordinate:
方法。
Wain是對的如果你已經在這個區域內,你必須自己弄清楚 通常你會有當前的座標,你可以檢查該區域是否包含該座標 – k1th
多次containsCoordinate:返回NO並且didEnterRegion不會被調用 –
第一個結論是didEnterRegion
與其名稱一致地實現。 :)
在CLLocationManagerDelegate
實現這樣的事情:
- (void) locationManager: (CLLocationManager *) manager
didStartMonitoringForRegion: (CLRegion *) region
{
if ([self insideRegion: region location: manager.location])
[self locationManager: manager
didEnterRegion: region];
}
我建議你使用此代碼
[locationManager requestStateForRegion:region];
,並使用委託方法didDetermineState:檢查狀態是CLRegionStateInside或CLRegionStateOutside。
這是完美的答案,containsCoordinate返回錯誤的值多次 –
返回所有位置的「unknown」:( –
從蘋果公司的文檔:
地理區域的監測後 登記授權的應用程序會立即開始。但是,不要期望馬上收到 事件,因爲只有邊界交叉纔會生成事件。 特別是,如果用戶的位置在註冊時間已經在 區域內,則位置管理器不會自動生成 事件。相反,您的應用必須等待用戶在生成事件並將其發送到 代表之前等待區域邊界穿過 。 要檢查用戶是否已位於區域的邊界 內,請使用 CLLocationManager類的requestStateForRegion:方法。
所以我落得這樣做的:
#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];
}
didEnterRegion是一個閾值事件,進入一個區域時觸發。想一想:一個事件表明你在一個區域內會不斷髮射。 – magma
我不認爲它會持續發射。當創建一個新的位置監視器時,它會檢查用戶當前是否在區域內並激發'didEnterRegion'(它在技術上從'null - > value'而不是'value - > value' - 可能會被解釋爲「進入」國際海事組織 –