2013-02-20 28 views
0

我在我的应用程序中遇到core location逻辑问题。以下代码位于rootviewcontroller中。它应该测试以查看,如果用户有允许的gps /位置服务开启或关闭为应用程序,然后作出决定,并移动到两个view controllers之一。如何解决iOS应用程序中的核心位置逻辑

应用程序启动时应运行此测试。此外,如果用户进入device Settings并更改Privacy > Location Services > App Name > ON/OFF并重新启动应用程序,则应该再次执行该过程。

这里是我到目前为止的代码:

#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]; 

} 

感谢您的帮助。

供参考:我以前曾问过这个问题,并认为它已解决。它仍然坚持,但我不知道它...

+0

当你运行这个时会发生什么? – paulmelnikow 2013-02-20 15:54:07

回答

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