使用另一个 class 获取位置在调用函数 swift 时不起作用

Getting location using another class not working when calling function swift

我之前使用 ViewController class 来获取用户更新,但现在在扩展应用程序时我需要将其移动到另一个 class 简单地处理所有位置更新。这是我现在使用的代码:

class ViewController: UIViewController, UITextFieldDelegate {

    @IBAction func pickMeUpButton(sender: AnyObject) {

        sendPushNotificationController().sendPushNotification("sendRequest",userLat: defaults.stringForKey("userLat")!, userLong: defaults.stringForKey("userLong")! )
    }

    @IBOutlet var numberForPickup: UITextField!

    let defaults = NSUserDefaults.standardUserDefaults()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.numberForPickup.delegate = self

        getLocationController().initLocation()
    }

所以我制作了另一个名为 getLocationController 的 class,它带有一个应该启动位置更新的初始化函数。这是代码:

class getLocationController: UIViewController, CLLocationManagerDelegate {

var locationManager: CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func initLocation(){

    print("Im in here")

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
}


func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {

    print("In location")

    if UIApplication.sharedApplication().applicationState == .Active {

        print("App in Foreground")

    }else {

        let Device = UIDevice.currentDevice()
        let iosVersion = Double(Device.systemVersion) ?? 0

        let iOS9 = iosVersion >= 9
        if iOS9{
            locationManager.allowsBackgroundLocationUpdates = true;
            locationManager.pausesLocationUpdatesAutomatically = false;
        }
        //let iOS7 = iosVersion >= 7 && iosVersion < 8
        print("App is backgrounded. New location is %@", newLocation)
    }
}
}

现在打印了 initLocation 中的打印,但没有打印 didUpdateLocations 中的打印。我在 ViewController class 中使用了完全相同的代码,它运行得非常好。现在,当我试图将它移动到另一个 class 时,它现在实际上是 phone 上的一个视图,但只是一个助手 class 它不起作用。有什么想法吗?

我没有看到您在 ViewController 中的任何地方将 getLocationController 分配给变量。这意味着 getLocationController 会超出范围并被销毁,不是吗?这可以解释为什么不调用回调 didUpdateToLocation。

尝试:

class ViewController: UITextFieldDelegate {

@IBAction func pickMeUpButton(sender: AnyObject) {

    sendPushNotificationController().sendPushNotification("sendRequest",userLat: defaults.stringForKey("userLat")!, userLong: defaults.stringForKey("userLong")! )
}

@IBOutlet var numberForPickup: UITextField!

let defaults = NSUserDefaults.standardUserDefaults()

var glc:getLocationController // is this how it is in Swift?!

override func viewDidLoad() {
    super.viewDidLoad()

    self.numberForPickup.delegate = self

    glc = getLocationController()
    glc.initLocation()
}