【发布时间】:2018-06-01 18:22:12
【问题描述】:
我在 swift 2 中有一个项目。当我第一次启动应用程序时,初始屏幕上会出现三种不同类型的权限弹出窗口(推送通知、位置、照片)。我已经添加了位置和照片的权限在 info.plist 中
问题是当应用程序启动时,一个(位置)弹出窗口出现并消失,无需任何点击,然后其他(照片)弹出窗口出现并在几秒钟后消失而无需任何点击。几秒钟后,弹出窗口一个接一个出现,现在弹出窗口一直显示在屏幕上,直到我点击任何一个选项。
我只想在用户点击按钮时显示一次权限弹出窗口。我已经搜索过它,但我找到的所有解决方案都在最新版本的 swift 中。对此的任何建议表示赞赏。
import CoreLocation
private var locationManager: CLLocationManager!
private var locationHandler: ((location: CLLocation?,error: NSError?) -> Void)?
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
func requestCurrentLocation(completionHandler:((location: CLLocation?,error: NSError?)->Void)!) {
locationHandler = completionHandler
if #available(iOS 9.0, *) {
locationManager.requestLocation()
} else {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let locationHandler = locationHandler, let location = locations.first {
locationHandler(location: location, error: nil)
self.locationHandler = nil
locationManager.stopUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
if let locationHandler = locationHandler {
locationHandler(location: nil, error: error)
self.locationHandler = nil
}
}
【问题讨论】:
-
我认为不编写代码就不会出现权限对话框。仅在 plist 中添加权限不会显示对话框
-
@InderKumarRathore,请检查。我已经添加了位置代码。
-
您在哪里请求位置权限?
-
我有一个NSObject类型的类。上面所有的代码都是写在那个类里的。我在类的init方法中添加了请求位置
标签: ios iphone swift permissions