-->

How to resolve a core location logic in iOS app

2019-08-26 22:53发布

问题:

I'm having a problem with the core location logic in my app. The following code is in the rootviewcontroller. It should test to see, if the user has allowed gps/location services on or off for the app and then make a decision and move to one of two view controllers.

The app should run this test when it launches. Also if the user goes into the device Settings and changes the Privacy > Location Services > App Name > ON/OFF and relaunches the app then it should go through the process again.

Here's the code I have so far:

#import <CoreLocation/CoreLocation.h>

-(void) viewWillAppear:(BOOL)animated {

    CLController = [[CoreLocationController alloc] init];
    CLController.delegate = self;
    [CLController.locMgr startUpdatingLocation];

#ifdef DEBUG
    NSLog(@"RootViewController");
#endif        

    spinner = [[UIActivityIndicatorView alloc]
                                        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.center = self.view.center;
    spinner.hidesWhenStopped = YES;
    [self.view addSubview:spinner];
    [spinner startAnimating];

}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (status == kCLAuthorizationStatusDenied) {
        // denied

        [self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];
        [CLController.locMgr stopUpdatingLocation];

    }else if (status == kCLAuthorizationStatusAuthorized) {
        // allowed
        [self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];
        [CLController.locMgr stopUpdatingLocation];
    }
}

- (void)locationUpdate:(CLLocation *)location {
        //locLabel.text = [location description];

#ifdef DEBUG    
    NSLog(@"auth status is %u", [CLLocationManager authorizationStatus]);
#endif    
        [self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];     
}

- (void)locationError:(NSError *)error {

    [self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"SegueOffersNoGPS"])
    {
        self.navigationController.toolbarHidden=YES;

#ifdef DEBUG
        NSLog(@"auth status no GPS");
#endif

    }

    if ([[segue identifier] isEqualToString:@"SegueOffersGPS"])
    {
        self.navigationController.toolbarHidden=NO;

#ifdef DEBUG
        NSLog(@"auth status is GPS");
#endif

    }
}

-(void)viewDidDisappear:(BOOL)animated {
    [CLController.locMgr stopUpdatingLocation];
    [spinner stopAnimating];

}

thanks for any help.

FYI: I did ask this question before and thought it was resolved. It's still persisting though and I can't figure it out...

回答1:

if (![self locationManager] .location) {
        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];
        return;
    }