Checking location service permission on iOS

前端 未结 8 1828
有刺的猬
有刺的猬 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 13:02

    After a lot of investigation. I would recommend to display this message on a label and not on an alert view. because, there are a lot of cases to test against(user disables location service in general or just for app. delete app, reinstall).

    One of these cases causes your alert to show your message along with apple's alert message at the same time. your alert will be behind apple's alert. which is a confusing and un-logical behavior.

    I recommend the following:

    Swift 3:

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    
        switch status {
            case .notDetermined:
                Log.verbose("User still thinking granting location access!")
                manager.startUpdatingLocation() // this will access location automatically if user granted access manually. and will not show apple's request alert twice. (Tested)
            break
    
            case .denied:
                Log.verbose("User denied location access request!!")
                // show text on label
                label.text = "To re-enable, please go to Settings and turn on Location Service for this app."
    
                manager.stopUpdatingLocation()
                loadingView.stopLoading()
            break
    
            case .authorizedWhenInUse:
                // clear text
                label.text = ""
                manager.startUpdatingLocation() //Will update location immediately
            break
    
            case .authorizedAlways:
                // clear text
                label.text = ""
                manager.startUpdatingLocation() //Will update location immediately
            break
            default:
                break
        }
    }
    

    Objective-C:

    - (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
        switch (status) {
            case kCLAuthorizationStatusNotDetermined: {
                DDLogVerbose(@"User still thinking granting location access!");
                [locationManager startUpdatingLocation]; // this will access location automatically if user granted access manually. and will not show apple's request alert twice. (Tested)
            } break;
            case kCLAuthorizationStatusDenied: {
                DDLogVerbose(@"User denied location access request!!");
                // show text on label
                label.text = @"To re-enable, please go to Settings and turn on Location Service for this app.";
    
                [locationManager stopUpdatingLocation];
                [loadingView stopLoading];
            } break;
            case kCLAuthorizationStatusAuthorizedWhenInUse:
            case kCLAuthorizationStatusAuthorizedAlways: {
                // clear text
                label.text = @"";
                [locationManager startUpdatingLocation]; //Will update location immediately
            } break;
            default:
                break;
        }
    }
    

提交回复
热议问题