यदि ऐप चल रहा है और CLLocationManagerDelegate क्लास अग्रभूमि (यानी दृश्यमान) है तो didEnterRegions ट्रिगर करता है और मुझे NSLog और AlertView दोनों मिलते हैं। हालांकि, जब ऐप पृष्ठभूमि में होता है या अनिवार्य रूप से, यदि स्क्रीन कुछ भी दिखा रही है तो प्रतिनिधि वर्ग के अलावा मुझे कुछ भी नहीं मिलता है।didEnterRegion अग्रभूमि में काम करता है लेकिन पृष्ठभूमि या अन्य वीसी
मैंने प्लेस्ट में "आवश्यक पृष्ठभूमि मोड" के तहत "स्थान अपडेट के लिए ऐप रजिस्ट्रार" सेट किया है, हालांकि मुझे यकीन नहीं है कि यह भी आवश्यक है।
यहां मुझे लगता है कि प्रासंगिक कोड है हालांकि मैं गलत हो सकता हूं (और खुशी से और जोड़ दूंगा)। मुझे ध्यान रखना चाहिए कि देखने में सबकुछ डीडलोड को एक में लपेटा गया है, जो जांचता है कि क्षेत्र की निगरानी उपलब्ध है या सक्षम है या नहीं।
- (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 में:
- (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];
}
}
}
और क्योंकि:
- (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 कहते हैं, यहाँ मुख्य जगह है मुझे यह महसूस हो रहा है कि यह महत्वपूर्ण हो सकता है, यहां स्थान के लिए गेटर है प्रबंधक:
- (CLLocationManager *)locationManager {
NSLog(@"MapViewController - locationManager");
if (_locationManager) {
return _locationManager;
} else {
_locationManager = [[CLLocationManager alloc] init];
return _locationManager;
}
}
अद्यतन 1: ऐप्पल मंचों (जहां मैंने क्रॉसपोस्ट किया) के माध्यम से किसी ने उल्लेख किया कि अलर्टव्यू केवल अग्रभूमि में दिखाएगा। फिर भी एनएसएलओजी आग नहीं लगाता है। मुझे लगता है कि काम करना चाहिए।
क्या आप उस कोड को शामिल कर सकते हैं जहां आप startMonitoringForRegion को कॉल कर रहे हैं? अभी शामिल कोड यह नहीं दिखाता है और केवल महत्वपूर्ण स्थान परिवर्तन से संबंधित कॉलबैक विधियों को ट्रिगर करेगा। और उन घटनाओं को मेरे अनुभव में सिम्युलेटर से नहीं आग लग जाएगी। –