Reputation: 16430
It is possible to add location awareness to an application when it's closed?
For example I'd like to create a proximity service that works when my application is closed and pushes a local notification to the user when he reaches an area.
I read that with CLRegion I can achieve a similar result but only whenever the application is active (or in background).
I need something similar to a remote notification service activated by the user location. But it sounds really strange. Is it possible to have a similar service in iOS?
Upvotes: 2
Views: 1366
Reputation: 14687
There are 2 options:
One is the significant location changes service which John mentioned above.
The other one is the startRegionMonitoring
Both are covered in the Location Awarness Documentation From Apple
Both can open an terminated (closed) app for a brief moment and you can deliver a local push notification to alert the user. The app cannot get in a normal state, but if the user clicks the notification the app will open, so you can achieve your goal.
Here is a sample code on intercepting the event in the didFinishLaunchingWithOptions delegate and firing a local notification. Normally you should not fire the notification here, but rather reinitialize your location manager, get the new position and display the message (local notification) for the area the user did enter...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//see if application was launched from a location event!
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
//if so, we need to reinitialize our location manager!
[[LocationManager sharedLocationManager] startSignificantLocationMonitoring];
//fire a notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
notification.timeZone = timezone;
notification.alertBody = @"app did wake up";
notification.alertAction = @"OK";
notification.soundName = @"yes.caf";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
Upvotes: 2
Reputation: 1679
I believe you can use the SignificantChange API to wake an app that has been closed by the system (or crashed). But you should still look into the region monitoring because it may be closer to what you actually want.
and the relevant section:
If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.
When you set up your CLLocationManager just call
- (void)startMonitoringSignificantLocationChanges
and handle any events in the typical fashion as a delegate callback
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
be aware that if the app was terminated, you will also get a call back to your app delegate did finish launching method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I'm also pretty sure that a callback in this case will include a flag to notify you that it was launched as a result of a location update.
Upvotes: 2
Reputation: 5157
I don't think you can do ANYTHING from a closed application in iOS. You can do a very limited number of things from the background (an inactive state), but I'm not aware of ANYTHING you can do from a closed application. I don't really see what an app could do while not running anyway ;-).
Upvotes: 0