如何在iOS应用中解决核心位置逻辑 [英] How to resolve a core location logic in iOS app

查看:96
本文介绍了如何在iOS应用中解决核心位置逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中核心位置逻辑有问题。以下代码位于 rootviewcontroller 中。它应该进行测试,以查看用户是否为该应用程序允许/关闭了gps /定位服务,然后做出决定并移至两个视图控制器

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.

应用在启动时应运行此测试。同样,如果用户进入设备设置并更改 Privacy>定位服务>应用名称>打开/关闭并重新启动应用程序,然后它应该再次通过该过程。

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

}

感谢您的帮助。

仅供参考:我之前曾问过这个问题,并认为它已解决。虽然它仍然持续存在,但我无法弄清楚...

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

推荐答案

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

这篇关于如何在iOS应用中解决核心位置逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆