- Home /
Can I avoid all unity functions/initializations from occurring when my app launches from the background?
I'm using CLLocationManager's startMonitoringSignificantLocationChanges via a unity plugin to relaunch the app whenever there's a significant location change. Unity's compiled code is preventing the app from reaching didUpdateLocation when it is relaunched from the background after the app is killed.
If I call exit(0) right after I initialize a location manager via didFinishLaunchingWithOptions, significant location change service will keep working. However, if I let it continue, significant location change will only trigger once and never work again unless I relaunch the app. Because of this I'm almost sure unity code is crashing at some point or killing my location service when my app is launching from the background.
This is the code I've added in UnityAppController.mm If I remove exit(0); the app will only wake up from a significant location chance once. However it's there, it'll keep working. I don't want the app to actually exit since I need the location manager to sent it's location to didUpdateLocation so that I get the new location.
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey] != nil) {
LocationManager* locationManager = [[LocationManager alloc] init];
[locationManager startSignificantChangeUpdates];
exit(0);
}
#pragma mark - StartSignificantChangeUpdates
- (void)startSignificantChangeUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
if([locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)])
[locationManager setAllowsBackgroundLocationUpdates:YES];
[locationManager setDelegate:self];
[locationManager startMonitoringSignificantLocationChanges];
[locationManager requestLocation];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//Establish notification details
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = 0;
notification.alertBody = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
Your answer
