locationManager 授权不起作用
locationManager authorization not working
我有一个应用程序,我正在尝试获取 LocationManager 显示的授权。我让它一直在工作,但后来在使用时尝试了。现在我无法让任何一个工作
我已下单
NSLocationWhenInUseUsageDescription 字符串启用播放
在我的 Info.plist
在我的应用委托中,我已将
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate= self;
return YES;
}
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorized){
NSLog(@"ok");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedAlways){
NSLog(@"Always");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
NSLog(@"WhenInUse");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusRestricted){
NSLog(@"restricted");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
NSLog(@"denid");
AlertView2 = [[UIAlertView alloc] initWithTitle:@"App Permission Denied"
message:@"Please go to Settings/Privacy/Location Services to enable Always for this app to function properly."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[AlertView2 show];
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
NSLog(@"undetermined");
[self.locationManager requestWhenInUseAuthorization];
}
}
我不确定这是我原来的配置,但我已经尝试了所有可能的配置,虽然显然不是。
谁能帮我解决这个问题。我更喜欢 WhenInUse 但此时我会接受任何东西
顺便说一下,我确实收到了 NSLog 未确定的回调,但未调用包含的代码。
感谢您的帮助。
几个想法:
我建议您在请求许可之前设置 delegate
。
什么是 authorizationStatus
在您请求许可之前?在请求许可之前,您应该检查一下,因为如果他们已经拒绝了许可,您就想告诉他们修复设置。如果权限先前被拒绝,则权限请求不会产生任何结果(包括状态更改)。
试试这个.....
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 1000.0f;
[locationManager requestAlwaysAuthorization];
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
所以我正式是个白痴。
我看着我的 info.plist 就在那里。我复制并粘贴了 Alway Description 并将其更改为 WhenInUse,当它不起作用时,我删除了原件并保留了副本并将其更改回 Always。但是,在展开该列以查看整个内容时,末尾附加了一个 -2,因此它的格式不正确,因此无法正常工作。一旦我删除了 -2,它就很好用了。我已经改变了原来的 post 来展示它是如何工作的。感谢您的帮助,我会确保解决您提出的问题。
我有一个应用程序,我正在尝试获取 LocationManager 显示的授权。我让它一直在工作,但后来在使用时尝试了。现在我无法让任何一个工作
我已下单 NSLocationWhenInUseUsageDescription 字符串启用播放 在我的 Info.plist
在我的应用委托中,我已将
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate= self;
return YES;
}
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorized){
NSLog(@"ok");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedAlways){
NSLog(@"Always");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
NSLog(@"WhenInUse");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusRestricted){
NSLog(@"restricted");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
NSLog(@"denid");
AlertView2 = [[UIAlertView alloc] initWithTitle:@"App Permission Denied"
message:@"Please go to Settings/Privacy/Location Services to enable Always for this app to function properly."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[AlertView2 show];
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
NSLog(@"undetermined");
[self.locationManager requestWhenInUseAuthorization];
}
}
我不确定这是我原来的配置,但我已经尝试了所有可能的配置,虽然显然不是。
谁能帮我解决这个问题。我更喜欢 WhenInUse 但此时我会接受任何东西
顺便说一下,我确实收到了 NSLog 未确定的回调,但未调用包含的代码。
感谢您的帮助。
几个想法:
我建议您在请求许可之前设置
delegate
。什么是
authorizationStatus
在您请求许可之前?在请求许可之前,您应该检查一下,因为如果他们已经拒绝了许可,您就想告诉他们修复设置。如果权限先前被拒绝,则权限请求不会产生任何结果(包括状态更改)。
试试这个.....
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 1000.0f;
[locationManager requestAlwaysAuthorization];
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
所以我正式是个白痴。 我看着我的 info.plist 就在那里。我复制并粘贴了 Alway Description 并将其更改为 WhenInUse,当它不起作用时,我删除了原件并保留了副本并将其更改回 Always。但是,在展开该列以查看整个内容时,末尾附加了一个 -2,因此它的格式不正确,因此无法正常工作。一旦我删除了 -2,它就很好用了。我已经改变了原来的 post 来展示它是如何工作的。感谢您的帮助,我会确保解决您提出的问题。