gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:17:20 26 4
gpt4 key购买 nike

我有一个使用设备位置的应用程序。如果他们允许该位置,我想运行我的方法 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com