我有一个应用程序,我想向其中添加一些基于位置的功能。它支持 iOS 7 和 8,但我遇到了一些问题。
我想仅当用户点击某个 viewController 上的某个按钮时请求位置权限。问题是应用程序启动后就会出现权限请求。更准确地说,在第一个 viewController 的 ViewWillAppear 和 ViewDidAppear 函数之间。
我之前尝试过:
plist 中有 NSLocationWhenInUseUsageDescription 键(适用于 iOS8)。 就代码而言:
self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.delegate = self; if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; } [self.locationManager startUpdatingLocation];
删除 plist 键 -> 这会导致 iOS8 中不再出现弹出窗口,但在 iOS7 中仍然会出现
目前与 CoreLocation 相关的每一个代码都被注释掉了,我仍然收到询问我权限的对话框(仅限 iOS7)
我不知道我还能尝试什么,所以非常感谢您的帮助
最佳答案
这就是 iOS 7(及更早版本)的工作原理。您不负责授权对话框:系统负责。你无法让它出现;系统看到您正在使用核心位置,就会代表您呈现它。这正是 iOS 8 所改变的事情。在 iOS 7 中您所能做的就是查看定位服务是否打开以及您的应用程序是否获得授权。
关于iOS7 和 iOS 8 位置服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28173973/