iOS CLLocationManager Authorization

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
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;
    }
}
 

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
I would put a boolean in NSUserDefaults to indicate the user has already been shown the label.
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 ?
 

xArtx

macrumors 6502a
Mar 30, 2012
764
1
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
 
Last edited:

dejo

Moderator
Staff member
Sep 2, 2004
15,981
447
The Centennial State
but it always drops to else block.
Always? As in, it never adds the label?

what should i do there ?
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.