problem when app launched in Core Location Event

Discussion in 'iOS Programming' started by Narendar Singh, Oct 31, 2012.

  1. Narendar Singh, Oct 31, 2012
    Last edited: Oct 31, 2012

    Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    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;
    }
    
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #3
    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
    }
     

Share This Page