iphone - 如何解决 iOS 应用程序中的核心位置逻辑

标签 iphone ios objective-c core-location cllocationmanager

我的应用程序中的 core location 逻辑有问题。以下代码在 rootviewcontroller 中。它应该测试以查看用户是否已允许打开或关闭该应用的 gps/定位服务,然后做出决定并移动到两个 View Controller 之一。

应用程序应在启动时运行此测试。此外,如果用户进入 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];

}

感谢您的帮助。

仅供引用:我之前确实问过这个问题并认为它已解决。它仍然存在,但我无法弄清楚...

最佳答案

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

关于iphone - 如何解决 iOS 应用程序中的核心位置逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14984168/

相关文章:

iphone - 将 NSUserDefaults 转换为钥匙串(keychain)?

ios - 在 initWithCoder : 上查看 1000x1000

iphone - 如何在2部分中激活tableView didSelectRowAtIndexPath?

ios - UICollectionView 在 iOS 11 上覆盖全屏

iphone - 在 iOS 6 的 TableView 中对数组进行排序

objective-c - ICMP 和 iPhone SDK

ios - 在 mapView 中加载区域的所有图 block

ios - 从 iPhone 5/5s 相机拍摄的 UIImage 有多少点

ios - 如何隐藏 UITabBar 项目?

ios - 如何在一个 UITableView 中维护不同布局的单元格