CLLocationManager Authorization

Discussion in 'iOS Programming' started by erdinc27, May 16, 2013.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #1
    hey all. i use CLLocationManager in my project. it works good for now. what my problem is when the user doesn't allow CLLocationManager then i place a UILabel on the view. but it is seen every time when the application is launched. i want to it will be seen only once when the user doesn't allow the CLLocationManager. how i can fix my code ? here how it looks.
    Code:
    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
        
        if (status == kCLAuthorizationStatusDenied)
        {
            UILabel *lblWarning=[[UILabel alloc] initWithFrame:self.lblCurrenTime.frame];
            [self.middleView addSubview:lblWarning];
            lblWarning.center=self.middleView.center;
            lblWarning.text=@"Select a Name";
            lblWarning.font=[UIFont fontWithName:@"Helvetica-Bold" size:17];
            lblWarning.backgroundColor=[UIColor clearColor];
            lblWarning.textColor=[UIColor whiteColor];
            lblWarning.textAlignment=NSTextAlignmentCenter;
            lblWarning.tag=999;
        }
    }
    
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I would put a boolean in NSUserDefaults to indicate the user has already been shown the label.
     
  3. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #3
    thanks for the reply. i did something like that.
    Code:
    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
        BOOL isFirstTime=[[NSUserDefaults standardUserDefaults] boolForKey:@"appSituation"];
        
        if (status == kCLAuthorizationStatusDenied)
        {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"appSituation"];
            
            if (isFirstTime)
            {
                UILabel *lblWarning=[[UILabel alloc] initWithFrame:self.lblCurrenTime.frame];
            [self.middleView addSubview:lblWarning];
            lblWarning.center=self.middleView.center;
            lblWarning.text=@"Select a Name";
            lblWarning.font=[UIFont fontWithName:@"Helvetica-Bold" size:17];
            lblWarning.backgroundColor=[UIColor clearColor];
            lblWarning.textColor=[UIColor whiteColor];
            lblWarning.textAlignment=NSTextAlignmentCenter;
            lblWarning.tag=999;
                
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"appSituation"];
                
            }
            else
            {
                NSLog(@"SEEN BEFORE");
            }
            
            
        
        }
    
    }
    
    
    but it always drops to else block. what should i do there ?
     
  4. xArtx, May 22, 2013
    Last edited: May 22, 2013

    xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #4
    Authorisation status isn't granted or denied until the user actually allows or disallows it.
    Code:
    if (status == kCLAuthorizationStatusDenied) {} else {}
    
    Your if branch assumes one or the other is always true when it isn't
    Have a look at
    Code:
    kCLAuthorizationStatusNotDetermined
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Always? As in, it never adds the label?

    What do you want to happen in that case?

    Also, "appSituation" seems like a confusing key name, to me. I would be more explicit about what that setting represents. As in, @"selectANameLabelAlreadyShown".

    And be sure to look into what xArtx is warning about. CLAuthorizationStatus is not a boolean but can be 4 possible values.
     

Share This Page