gpt4 book ai didi

ios - Xcode 6.2 beta 和 iOS 8 模拟器 : location manager authorization dialogue fails to appear: bug or failure to understand?

转载 作者:行者123 更新时间:2023-12-01 16:36:04 26 4
gpt4 key购买 nike

我找到了以下 solution解释如何在 iOS 8 中使用定位服务。不幸的是,我只有一个 iPhone 6 模拟器而不是真正的设备。我尝试在 viewDidLoad 方法中添加此代码,但从未显示授权对话框。为什么是这样?

我在下面添加了一个断点,它被执行但不显示对话框..:

    // Will open an confirm dialog to get user's approval
[locationManager requestAlwaysAuthorization];

这是完整的 viewDidLoad 方法:
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Initialize location manager
locationManager = [CLLocationManager new];
locationManager.delegate = self;

float system = [[[UIDevice currentDevice] systemVersion] floatValue ];

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways
) {
// Will open an confirm dialog to get user's approval
[locationManager requestAlwaysAuthorization];
} else {
[locationManager startUpdatingLocation]; //Will update location immediately
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

PS:我还修改了 info.plist 如下:

enter image description here

PPS:

这就是我(在同一个 ViewController 中)实现 其余部分的方式CLLocationManagerDelegate 协议(protocol) :
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
NSLog(@"User still thinking..");
} break;
case kCLAuthorizationStatusDenied: {
NSLog(@"User dislikes your app");
} break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
[locationManager startUpdatingLocation];
} break;
default:
break;
}
}

- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}

编辑 2:我按照答案中的建议添加了以下代码块。代码已执行,但对话未能出现。
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
[locationManager requestAlwaysAuthorization];
}
} break;
case kCLAuthorizationStatusDenied: {
NSLog(@"User dislikes your app");
} break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
[locationManager startUpdatingLocation];
} break;
default:
break;
}
}

最佳答案

将此添加到 didChangeAuthorizationStatus:

case kCLAuthorizationStatusNotDetermined: {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestAlwaysAuthorization];
}

编辑
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways
) {
// Will open an confirm dialog to get user's approval
[locationManager requestAlwaysAuthorization];
}

[locationManager startUpdatingLocation]; //Will update location immediately

关于ios - Xcode 6.2 beta 和 iOS 8 模拟器 : location manager authorization dialogue fails to appear: bug or failure to understand?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28149705/

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