Reputation: 1
I am trying to write an app in iOS for the iPhone 4 using significant location change that fires a local notification each time the location delegate receives a location update. My code is posted below. I resign the app and close it out of the background. As long as the phone is awake (the screen is lit up), the application works well, firing the notifications, but when I put the phone to sleep (the screen is black), I no longer receive notifications until I wake the phone up by pressing the home button, receiving a text, etc. Then the notification is fired. What do I need to do in order for location updates to register even when the device is asleep? Thanks in advance.
SigLoc.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface SigLocViewController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
SigLoc.m
#import "SigLocViewController.h"
@implementation SigLocViewController
@synthesize locationManager;
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringSignificantLocationChanges];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *alarm = [[[NSClassFromString(@"UILocalNotification") alloc] init] autorelease];
if (alarm) {
alarm.fireDate = nil;
alarm.repeatInterval = 0;
alarm.alertBody = @"Location Update";
alarm.soundName = UILocalNotificationDefaultSoundName;
alarm.hasAction = NO;
[app presentLocalNotificationNow:alarm];
}
}
Upvotes: 0
Views: 1763
Reputation: 14154
My guess is your view is not active when your app goes into the background, so it has no way to get the notifications. I would assign your app delegate as a location manager delegate, and then pass the callbacks to your SigLoc location manager delegate.
I had a similar issue trying to receive the callbacks, which didn't resolve until I moved the location manager delegate to my app delegate. I would start there and work your way back to your method.
Upvotes: 0