How to check if location services are enabled for a particular app prior to iOS 4.2?

后端 未结 3 1351
灰色年华
灰色年华 2020-12-23 16:57

How can I check if the user has allowed location for mu app? Normally I would use authorizationStatus method of the CLLocationManager class, but it

3条回答
  •  庸人自扰
    2020-12-23 17:37

    I've combined two techniques in the code below

        MKUserLocation *userLocation = map.userLocation;
        BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
        BOOL locationAvailable = userLocation.location!=nil;
    
        if (locationAllowed==NO) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                                                            message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                           delegate:nil 
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        } else {
            if (locationAvailable==NO) 
                [self.map.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
        }
    

提交回复
热议问题