Checking location service permission on iOS

前端 未结 8 1825
有刺的猬
有刺的猬 2020-12-07 12:32

How can I check if location service is enabled for my app?

I have 2 storyboards and I want to check location service. If location service enabled for my app, I want

8条回答
  •  青春惊慌失措
    2020-12-07 12:49

    Tested on iOS 9.2

    For getting location updates we should always check

    • Location services enabled on user's iOS Device and
    • Location services enabled for particular app

    and launching user on correct settings screen to enable

    Launch iOS Device Location Settings page

    Step.1 Go to Project settings --> Info --> URL Types --> Add New URL Schemes

    Step.2 Use below code to launch direct phone's location settings page: (Note: The URL Scheme is different in iOS 10+, we check the version as stated here)

     #define SYSTEM_VERSION_LESS_THAN(v)  ([[[UIDevice 
     currentDevice] systemVersion] compare:v options:NSNumericSearch] == 
     NSOrderedAscending)
    
     //Usage
    NSString* url = SYSTEM_VERSION_LESS_THAN(@"10.0") ? @"prefs:root=LOCATION_SERVICES" : @"App-Prefs:root=Privacy&path=LOCATION";
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
    

    Launch Application Location Settings page

    Use below code to launch direct application's location settings page

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    

    Here is the full code example :

    #define SYSTEM_VERSION_LESS_THAN(v)  ([[[UIDevice 
     currentDevice] systemVersion] compare:v options:NSNumericSearch] == 
     NSOrderedAscending)
    
    
    CLLocationManager *locationManager;
    
    -(void) checkLocationServicesAndStartUpdates
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
        {
            [locationManager requestWhenInUseAuthorization];
        }
    
        //Checking authorization status
        if (![CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
        {
    
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled!"
                                                                message:@"Please enable Location Based Services for better results! We promise to keep your location private"
                                                               delegate:self
                                                      cancelButtonTitle:@"Settings"
                                                      otherButtonTitles:@"Cancel", nil];
    
            //TODO if user has not given permission to device
            if (![CLLocationManager locationServicesEnabled])
            {
                alertView.tag = 100;
            }
            //TODO if user has not given permission to particular app
            else
            {
                alertView.tag = 200;
            }
    
            [alertView show];
    
            return;
        }
        else
        {
            //Location Services Enabled, let's start location updates
            [locationManager startUpdatingLocation];
        }
    }
    

    Handle the user click respone, and launch correct location settings

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    
        if(buttonIndex == 0)//Settings button pressed
        {
            if (alertView.tag == 100)
            {
                //This will open ios devices location settings
                NSString* url = SYSTEM_VERSION_LESS_THAN(@"10.0") ? @"prefs:root=LOCATION_SERVICES" : @"App-Prefs:root=Privacy&path=LOCATION";
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
            }
            else if (alertView.tag == 200)
            {
                //This will opne particular app location settings
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            }
        }
        else if(buttonIndex == 1)//Cancel button pressed.
        {
            //TODO for cancel
        }
    }
    

提交回复
热议问题