位置管理器委托不称为iOS swift

时间:2016-04-08 10:24:21

标签: ios

我在info plist中设置了密钥NSLocationAlwaysUsageDescription,NSLocationWhenInUseUsageDescription并运行下面的代码,没有调用委托,并且短时间内出现位置弹出:

类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)
}

}

1 个答案:

答案 0 :(得分:3)

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

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

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