How to resolve a core location logic in iOS app

元气小坏坏 提交于 2019-12-25 02:38:24

问题


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;
    }


来源:https://stackoverflow.com/questions/14984168/how-to-resolve-a-core-location-logic-in-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!