您必須注意訪問用戶設備位置的權限。因此該應用程序能夠跟蹤用戶的位置。
您可以通過以下做到這一點:
- 中的.plist文件添加一個關鍵
NSLocationWhenInUseUsageDescription
。這是鑰匙的類型值爲String
。您必須注意不要在此處使用String
以外的其他類型。
向用戶訪問的用戶位置之前,添加下面一行。
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
下面的代碼將開始監視區:
// Tell location manager to start monitoring for the region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
而且覆蓋下面的方法,以確保區域發現:
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
// We entered a region!
NSLog(@"Entered in region");
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
// Exited the region
NSLog(@"Exited from region");
}
我認爲這是一個自定義的方法,你會得到其他方法來觸發這個方法的最後一行。你在didEnterRegion時檢查它。 – Larme 2015-02-11 13:33:01