CLLocationManager - 授权提示消失了吗?
CLLocationManager - Authorization prompt goes away?
我尝试构建一个 locationHelper-class 来处理使用 CLLocation 的方法,这样我就不必在每个视图控制器中重写它们。我的 LocationHelper class 有一个方法 checkStatus() 来检查用户是否已经授予使用他的位置的授权。但是,如果我调用该方法并且用户未授予权限,则提示会出现,但会在提示出现后 2 秒消失。我认为问题出在 class 的实现上,因为如果我在 ViewController 中编写代码,提示会一直存在,直到用户做出决定。
我的LocationHelper.swift:
import Foundation
import CoreLocation
class LocationHelper: NSObject, CLLocationManagerDelegate{
var locationManager: CLLocationManager!
var delegate: CLLocationManagerDelegate!
override init(){
super.init()
self.locationManager = CLLocationManager()
locationManager.delegate = self
}
func requestPermission() -> Void {
self.locationManager?.requestWhenInUseAuthorization()
}
//func getLocationInstance() -> CLLocationManager {
// return self.locationManager?
//}
func checkStatus() -> Void {
switch CLLocationManager.authorizationStatus() {
case .notDetermined:
// Request when-in-use authorization initially
print("not determined")
locationManager?.requestWhenInUseAuthorization()
case .restricted, .denied:
// Disable location features
print("status: fail")
case .authorizedWhenInUse:
// Enable basic location features
print("in use")
case .authorizedAlways:
print("always")
}
}
}
我的ViewController:
import UIKit
class LiveDataViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor(red: 0.12, green: 0.67, blue: 0.478, alpha: 1)
let locHelp = LocationHelper()
locHelp.checkStatus()
//locHelp LocationHelper = LocationHelper()
//locHelp.requestPermission()
}
如果有人能帮助我,我会很高兴。
尝试保存对位置助手的引用。
它可能会在视图加载后自动释放。
像这样:
import UIKit
class LiveDataViewController: UIViewController {
let locHelp = LocationHelper()
override func viewDidLoad() {
super.viewDidLoad()
locHelp.checkStatus()
}
}
我尝试构建一个 locationHelper-class 来处理使用 CLLocation 的方法,这样我就不必在每个视图控制器中重写它们。我的 LocationHelper class 有一个方法 checkStatus() 来检查用户是否已经授予使用他的位置的授权。但是,如果我调用该方法并且用户未授予权限,则提示会出现,但会在提示出现后 2 秒消失。我认为问题出在 class 的实现上,因为如果我在 ViewController 中编写代码,提示会一直存在,直到用户做出决定。 我的LocationHelper.swift:
import Foundation
import CoreLocation
class LocationHelper: NSObject, CLLocationManagerDelegate{
var locationManager: CLLocationManager!
var delegate: CLLocationManagerDelegate!
override init(){
super.init()
self.locationManager = CLLocationManager()
locationManager.delegate = self
}
func requestPermission() -> Void {
self.locationManager?.requestWhenInUseAuthorization()
}
//func getLocationInstance() -> CLLocationManager {
// return self.locationManager?
//}
func checkStatus() -> Void {
switch CLLocationManager.authorizationStatus() {
case .notDetermined:
// Request when-in-use authorization initially
print("not determined")
locationManager?.requestWhenInUseAuthorization()
case .restricted, .denied:
// Disable location features
print("status: fail")
case .authorizedWhenInUse:
// Enable basic location features
print("in use")
case .authorizedAlways:
print("always")
}
}
}
我的ViewController:
import UIKit
class LiveDataViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor(red: 0.12, green: 0.67, blue: 0.478, alpha: 1)
let locHelp = LocationHelper()
locHelp.checkStatus()
//locHelp LocationHelper = LocationHelper()
//locHelp.requestPermission()
}
如果有人能帮助我,我会很高兴。
尝试保存对位置助手的引用。
它可能会在视图加载后自动释放。
像这样:
import UIKit
class LiveDataViewController: UIViewController {
let locHelp = LocationHelper()
override func viewDidLoad() {
super.viewDidLoad()
locHelp.checkStatus()
}
}