[英]Xcode 8 iOS Simulator is not prompting for location services authorization in my Swift 3 project
[英]Xcode 6.2 beta and iOS 8 simulator: location manager authorization dialogue fails to appear: bug or failure to understand?
我找到了以下解决方案,解释了如何在iOS 8中使用定位服务。不幸的是,我只有一个iPhone 6模拟器,而没有真正的设备。 我试图将这段代码添加到viewDidLoad方法中,但从未显示授权对话框。 为什么是这样?
我在下面添加了一个断点,它被执行了,但没有显示出Dialouge ..:
// 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,如下所示:
PPS:
这就是我在同一个ViewController中实现其余CLLocationManagerDelegate协议的方式 :
#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
我认为您的代码可能没有任何问题。 这是我正在使用的代码:-
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
if(IS_OS_8_OR_LATER) {
[locationManager requestAlwaysAuthorization];
}
[locationManager startUpdatingLocation];
您可能不了解授权对话框的工作方式。 供您参考,授权对话框只会出现一次(您允许还是不允许)。 之后,您可以从“设置”中更改授权。
如果要重新显示授权对话框,则应从iPhone模拟器中删除该应用程序(长按该应用程序并点击X按钮)。 当您将应用程序从XCode重新启动到iPhone Simulator中时,只会重新显示授权对话框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.