Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
I have an application which register itself for Significant Location Changed Services
when it goes to background.

As per docs reading over the net and my understanding, I have written following code in
didFinishLaunchingWithOptions method and I was expecting when app is launches in background and
launched with core location event then it should return and should not create any user interface:

Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];

/*
[COLOR="Red"]
If I normally launch the app by pressing its icon from home screen, then application goes in this if block, 
strange !! it should not do like this because app is not launched by Core Location Event and also not in 
background. 

Side effect of this on my app is that, because app is going in if block it immediately returns and shows 
BLACK SCREEN because it could not able to show view that is added to window.    

[/COLOR]
*/
    if (locationValue && [UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
	{

#ifdef DEBUG
        LogToFile(@" App was launched in response to a CoreLocation event. ");
        [Helper showAlertViewWithTitle: @"Significant Location"
							   message: @"App was launched in response to a CoreLocation event."
							  delegate: nil
						  buttonTitles: [NSArray arrayWithObject: @"OK"]
					 cancelButtonIndex: 0];
#endif
        
        singnificantLocationManager = [[CLLocationManager alloc] init];
		singnificantLocationManager.delegate = self;		
		[singnificantLocationManager startMonitoringSignificantLocationChanges];		
		return YES;      
	}


// adding view to window
    navigationController = [NavigationController sharedNavigationController];
    [window addSubview:navigationController.view];

    [self.window makeKeyAndVisible];
    return YES;
}
 
Last edited:
Adding a subview to your window is not the preferred way to go about this. Instead, you should be setting the window's rootViewController to your view controller.
 
Adding a subview to your window is not the preferred way to go about this. Instead, you should be setting the window's rootViewController to your view controller.

Thanks for quick reply, everything works well with my current code by adding subview instead of you suggested. I'll do that change later on.

But my problem is that why in normal app launch app goes in if block, how both of the condition can be true?

----------

Code:
if (locationValue && [UIApplication sharedApplication].applicationState 
      == UIApplicationStateBackground)
{
 // Application should enter in this block by launching app from home screen icon
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.