如果應用程序正在運行,並且CLLocationManagerDelegate類是前景(即可見),則didEnterRegions觸發器,我同時得到NSLog和AlertView。然而,當應用程序處於後臺時,我什麼也得不到,或者實際上,如果屏幕顯示除委託類以外的任何內容。didEnterRegion在前臺工作,但不在後臺或其他VC
我已經在plist的「所需的背景模式」下設置了「位置更新的應用寄存器」,儘管我不確定這是甚至必要的。
這是我認爲是相關的代碼,雖然我可能是錯的(並會很樂意添加更多)。我應該注意,viewDidLoad中的所有內容都包含在一個if中,用於檢查區域監視是否可用並已啓用。
- (void)viewDidLoad
{
NSLog(@"MapViewController - viewDidLoad");
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
self.locationManager.delegate = self;
[self.locationManager startMonitoringSignificantLocationChanges];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"MapViewController - didEnterRegion");
NSLog(@"MVC - didEnterRegion - region.radius = %f", region.radius);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"entered region..." message:@"You have Entered the Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
alert.tag = 2;
[alert show];
}
這裏是我得到被監控區域的名單,在AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// other code
NSLog(@"LISTING ALL REGIONS MONITORED");
NSArray *regions = [self.locationManager.monitoredRegions allObjects];
if (!regions) {
NSLog(@"no regions found");
} else {
NSLog(@"got %d monitored regions", [regions count]);
for (int i = 0; i < [regions count]; i++) {
CLRegion *region = [regions objectAtIndex:i];
NSLog(@"region %d's identifier = %@", i, region.identifier);
NSLog(@"region: radius: %@", region.radius);
}
}
// other code
}
我打電話startMonitoringForRegion兩次,這裏的主要場所:
- (void)doneButtonTapped {
NSLog(@"doneButtonTapped");
if (self.locationIdentifier) {
if ([CLLocationManager regionMonitoringEnabled] && [CLLocationManager regionMonitoringAvailable]) {
// core data setup
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"LocationReminder" inManagedObjectContext:self.managedObjectContext];
fetchRequest.entity = entityDescription;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"locationIdentifier == %@", self.locationIdentifier];
fetchRequest.predicate = predicate;
NSError *error;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (results) {
// get the LocationReminder
LocationReminder *retrievedReminder = [results objectAtIndex:0];
retrievedReminder.audioURI = [[[self.audioPlayers objectAtIndex:self.selectedCell] url] absoluteString];
retrievedReminder.userRecording = nil;
// start monitoring it's region
NSArray *coordinateArray = [retrievedReminder.locationIdentifier componentsSeparatedByString:@", "];
CLLocationCoordinate2D coordinate = {[[coordinateArray objectAtIndex:0] doubleValue], [[coordinateArray objectAtIndex:1] doubleValue]};
CLRegion *newRegion = [[CLRegion alloc] initCircularRegionWithCenter:coordinate radius:250.0 identifier:retrievedReminder.locationIdentifier];
NSLog(@"about to monitor region with radius: %f", newRegion.radius);
[self.locationManager startMonitoringForRegion:newRegion desiredAccuracy:kCLLocationAccuracyBest];
// save the LocationReminder
if (![self.managedObjectContext save:&error]) {
NSLog(@"hmm. no managed object context. must be something space-time going on");
} else {
NSLog(@"saved locationReminder, locationIdentifier = %@", retrievedReminder.locationIdentifier);
}
} else {
NSLog(@"ERROR: no LocationReminder retreived for predicate: %@", predicate);
}
}
// get the mapview controller off of the navigation stack
for (UIViewController *viewController in self.navigationController.viewControllers) {
if ([viewController isKindOfClass:[MapViewController class]]) {
MapViewController *mapVC = (MapViewController *)viewController;
mapVC.audioURI = [[[self.audioPlayers objectAtIndex:self.selectedCell] url] absoluteString];
[self.navigationController popToViewController:mapVC animated:YES];
}
}
}
而且由於我覺得這可能很重要,這裏是locationManager的獲取者:
- (CLLocationManager *)locationManager {
NSLog(@"MapViewController - locationManager");
if (_locationManager) {
return _locationManager;
} else {
_locationManager = [[CLLocationManager alloc] init];
return _locationManager;
}
}
更新1:通過蘋果論壇(我在那裏交叉)有人提到AlertView將只顯示在前臺。 NSLog也不會觸發。我假設應該工作。
您可以包含您調用startMonitoringForRegion的代碼嗎?目前包含的代碼沒有顯示,只會觸發與重大位置更改相關的回調方法。根據我的經驗,這些事件不會從模擬器中觸發。 –