gpt4 book ai didi

ios - 位置管理器委托(delegate)不叫 iOS swift

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:15 26 4
gpt4 key购买 nike

我在 info plist 中设置了键 NSLocationAlwaysUsageDescription、NSLocationWhenInUseUsageDescription 并运行下面的代码,委托(delegate)不会被调用,位置弹出窗口有时会出现很短的时间:

类 LocationFinder:NSObject,CLLocationManagerDelegate {

enum LocationSettingStatus : Int
{
case Value
}

class var sharedInstance: LocationFinder {

struct Static {
static var instance: LocationFinder?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = LocationFinder()
}

return Static.instance!
}

func checkStatusOfLocationSetting() -> Bool {

//LocationSettingStatus.Value = 1

if(CLLocationManager.authorizationStatus() == .NotDetermined || CLLocationManager.locationServicesEnabled() == false)
{
return false
}
else if(CLLocationManager.authorizationStatus() == .Denied || CLLocationManager.authorizationStatus() == .Restricted || CLLocationManager.locationServicesEnabled() == false )
{
return false
}
return true

}

func startUpdateLocation() {

let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}




func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}


func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

print(status)
}

最佳答案

您已将位置管理器声明为局部变量,因此它会在方法结束时被销毁:

func startUpdateLocation()  {
let locationManager = CLLocationManager()
locationManager.delegate = self
...

相反,将其设为实例变量。

关于ios - 位置管理器委托(delegate)不叫 iOS swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36497243/

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