ios - 查明是否启用了位置服务不起作用

标签 ios objective-c cllocationmanager

我有一个使用设备位置的应用程序。如果他们允许该位置,我想运行我的方法 getDataFromJson 并正常运行我的应用程序。如果他们否认,或者之前否认过,我希望向他们展示一个 View ,解释他们需要转到设置并允许它。

我有很多代码,但暂时不起作用。谁能帮忙解释一下问题出在哪里?

非常感谢!

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([CLLocationManager authorizationStatus] == YES) {
        //are enabled, run the JSON request
        [self getDataFromJson];
    } else {
        //is not enabled, so set it up
        NSLog(@"no");
        [locationManager location];
    };

}

-(CLLocationCoordinate2D) getLocation{

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];
    return coordinate;

}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusDenied) {
        //location denied, handle accordingly
        locationFailView.hidden = NO;
        mainView.hidden = YES;
    }
    else if (status == kCLAuthorizationStatusAuthorized) {
        //hooray! begin tracking
        [self getDataFromJson];
    }
}

//class to convert JSON to NSData
- (IBAction)getDataFromJson {
    CLLocationCoordinate2D coordinate = [self getLocation];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
    ...
}

最佳答案

+ (CLAuthorizationStatus)authorizationStatus

Return Value A value indicating whether the application is authorized to use location services.

Discussion The authorization status of a given application is managed by the system and determined by several factors. Applications must be explicitly authorized to use location services by the user and location services must themselves currently be enabled for the system. This authorization takes place automatically when your application first attempts to use location services.

+ (BOOL)locationServicesEnabled

Returns a Boolean value indicating whether location services are enabled on the device.

您可以检查这两个状态:locationServicesEnabled 和 authorizationStatus,然后决定应该使用哪个方法。

AuthorizationStatus 应该检查状态:

typedef enum {
   kCLAuthorizationStatusNotDetermined = 0,
   kCLAuthorizationStatusRestricted,
   kCLAuthorizationStatusDenied,
   kCLAuthorizationStatusAuthorized
} CLAuthorizationStatus;

但是你在 viewDidLoad 方法中检查是否等于 bool 值。

关于ios - 查明是否启用了位置服务不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17532804/

相关文章:

objective-c - copyItemAtPath、moveItemAtPath 和 createFileAtPath 似乎都创建了一个不可读的文件

ios - CoreLocation的区域监控系统定义的缓冲是什么?

iphone - 我如何在 iphone 中以相同的方法加载不同的 View

ios - 调试问题 - 值被隐藏

iphone - 检测 iOS 设备向右或向左或向上或向下移动

ios - 如何在 iOS 中以编程方式禁用位置服务?

ios - 为什么我更新的应用程序在启动时仅适用于 IOS8 崩溃?

iphone - 无法设置 NSString 属性,崩溃

ios - 实现不完整

ios - 仅从表行弹出 Watchkit 上下文菜单?