gpt4 book ai didi

iOS:应用程序在安装应用程序时不征求用户的许可。每次都获取 kCLAuthorizationStatusNotDetermined - Objective-c & Swift

转载 作者:IT王子 更新时间:2023-10-29 07:32:13 27 4
gpt4 key购买 nike

我正在尝试在我的 iOS 应用程序中获取用户位置。我首先在我的项目中包含了 corelocation 框架。然后点击一个按钮,我正在调用核心位置 api,如下所示。当我尝试在设备中安装它时,核心位置从不询问用户许可。当我尝试在单击按钮时获取位置时,我得到 kCLAuthorizationStatusNotDetermined 作为授权状态。请帮助我。我不知道发生了什么。

- (IBAction)fetchLessAccurateLocation:(id)sender {
[self.txtLocation setText:@""];

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 1000;

if ([self shouldFetchUserLocation]) {
[locationManager startUpdatingLocation];
}
}

这是我的 shouldFetchUserLocation 方法:

-(BOOL)shouldFetchUserLocation{

BOOL shouldFetchLocation= NO;

if ([CLLocationManager locationServicesEnabled]) {
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusAuthorized:
shouldFetchLocation= YES;
break;
case kCLAuthorizationStatusDenied:
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"App level settings has been denied" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}
break;
case kCLAuthorizationStatusNotDetermined:
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The user is yet to provide the permission" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}
break;
case kCLAuthorizationStatusRestricted:
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The app is recstricted from using location services." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}
break;

default:
break;
}
}
else{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The location services seems to be disabled from the settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}

return shouldFetchLocation;
}

这是我的核心位置委托(delegate)方法:

- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0){

NSLog(@"location fetched in delegate");

CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

if (abs(howRecent) < 15.0) {
// If the event is recent, do something with it.
NSLog(@"inside loop.... latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
}
NSLog(@"latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
[self.txtLocation setText:[NSString stringWithFormat:@"\nlatitude: %+.6f \nlongitude: %+.6f", location.coordinate.latitude, location.coordinate.longitude]];

[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];

if(locationManager!=nil){
locationManager.delegate= nil;
locationManager= nil;
}
}

最佳答案

iOS8 对 LocationsServices 的 API 进行了重大更改

假设[CLLocationManager locationServicesEnabled] 返回 YES,

首次启动 iOS 应用程序 [iOS7 和 iOS8] - locationMangers(CLLocationManager) authorizationStatus预设为

authorizationStatus(CLAuthorizationStatus) = kCLAuthorizationStatusNotDetermined

在 iOS7+ 中提示

启动位置管理器(CLLocationManager, Strong) 并设置委托(delegate)(CLLocationManagerDelegate)

现在提示用户使用位置服务并在设置> 隐私> 位置服务下列出应用程序,它必须调用任何位置服务方法,这取决于应用程序要求 - 例如,如果应用程序是一种以下的

位置更新 - [self.locationManager startUpdatingLocation]

RegionMonitoring - [self.locationManager startMonitoringForRegion:beaconRegion]

执行上述方法后,iOS 将立即提示用户请求接受在应用程序中使用位置服务,并且无论用户选择如何,应用程序都将列在“设置”>“隐私”>“位置服务”下。

在 iOS8+ 中提示

iOS8也是一样,首先推出了App Locations Services

authorizationStatus(CLAuthorizationStatus) = kCLAuthorizationStatusNotDetermined

iOS 8 我们获得了向用户显示提示的新方法

[self.locationManager requestAlwaysAuthorization] 
or
[self.locationManager requestWhenInUseAuthorization]

requestAlwaysAuthorization/requestWhenInUseAuthorization 可从 iOS8 获得。如果应用程序部署目标是 iOS7,则将其包装在 if block 下,以确保在 iOS7 中这不会导致应用程序崩溃。

if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization]; .
}

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}

非常重要###:

根据 iOS8,它必须包含说明应用为何使用 requestAlwaysAuthorization/requestWhenInUseAuthorization 的字符串在 info.plist 中包含与应用程序要求相关的任何这些属性

对于 kCLAuthorizationStatusAuthorizedAlways 包含键/值(字符串值)对

NSLocationAlwaysUsageDescription = App use Locations service mode Always

对于 kCLAuthorizationStatusAuthorizedWhenInUse 包括键/值(字符串值)对

NSLocationWhenInUseUsageDescription = App use Locations service mode In Use 

info.plist 的屏幕截图(以防有人对此感到困惑) enter image description here

关于iOS:应用程序在安装应用程序时不征求用户的许可。每次都获取 kCLAuthorizationStatusNotDetermined - Objective-c & Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664928/

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