开发者

Display iPhone alert from background mode when monitoring region boundary crossing

I have a question about displaying a UIAlertView when running in background mode as a result of a location region event. I have done an extensive trawl of similar questions on here and have downloaded the Apple Breadcrumb example but it doesn't attempt to display alerts.

My app switches to monitoring for regions just before it enters background mode with this line:

[self.locMan startMonitoringForRegion:targetRegion desiredAccuracy:100];

I then monitor for region entry and exit as follows:

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"Exited region");

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Region Boundary Crossed!" 
                                                    message:@"Exited region"
                                                   delegate:self 
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"Show",nil ]; 

[alertView show];
[alertView release];
}


-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region 
{
NSLog(@"Entered region"); 

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Region Boundary Crossed!" 
                                                    message:@"Entered region"
                                                   delegate:self 
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"Show",nil ]; 
[alertView show];
[alertView release];
}

I have run this using the simulator to confirm that it correctly switches to region monitoring. However, on the iPhone I have not been able to see an alert when running in the background mode but desired alerts are displayed 开发者_StackOverflowwhen reactivating the app, seemingly queued up and waiting.

In my info.plist file I have set 'Required background modes - Item 0' to 'App registers for location updates' and 'Required device capabilities - Item 0' to 'location-services' and 'Item 1' to 'GPS'.

Any help much appreciated!


UIAlertView will not work when your app is backgrounded. You should use UILocalNotifications when you app is running in the background.

Also be aware that with the desired accuracy you will drain the iPhone battery really quick. There is a special setting for CCLocationManager when running in the background, this will only get called on major change, which might not be accrued enough for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜