WatchKit CoreLocation issue

寵の児 提交于 2019-12-06 05:16:38

问题


I am trying to read current location coordinates in WatchKitExtension ExtensionDelegate. This does though not return any value.

The very same code used in WatchKitExtension InterfaceController does return the location. (tried this out of desperation as I could not find an error in the code)

I would need to perform this code in ExtensionDelegate as I would like to pass the retrieved location on to a ClockKit Complication.

Here the code in ExtensionDelegate: (after self.locationManager.requestLocation() the delegate functions didUpdateLocation / didFailWithError do not get called)

import WatchKit
import CoreLocation



class ExtensionDelegate: NSObject, WKExtensionDelegate, CLLocationManagerDelegate {

    private let locationManager = CLLocationManager()

    override init() {
        print("ExtensionDelegate: \(NSDate())  - init")
        super.init()
        self.getLocation()
    }


    func getLocation(){
        print("ExtensionDelegate: \(NSDate())  - getLocation")
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestLocation()
    }

...


    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("ExtensionDelegate: \(NSDate())  - locationManager didUpdateLocations")

        guard let mostRecentLocation = locations.last else { return }
        let place = mostRecentLocation.coordinate
        print("\(place)")
        manager.stopUpdatingLocation()
    }


    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("ExtensionDelegate: \(NSDate())  - locationManager didFailWithError")
        print("CL failed: \(error)")
    }


}

Here the very same code in InterfaceController and it works perfectly (didUpdateLocation does get called):

import WatchKit
import Foundation
import CoreLocation

class InterfaceController: WKInterfaceController, CLLocationManagerDelegate {

    private let locationManager = CLLocationManager()

    override func awakeWithContext(context: AnyObject?) {
        print("InterfaceController: \(NSDate())  - awakeWithContext")
        super.awakeWithContext(context)
        self.getLocation()
    }

    func getLocation(){
        print("InterfaceController: \(NSDate())  - getLocation")
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestLocation()
    }

   ...

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("InterfaceController: \(NSDate())  - locationManager didUpdateLocations")

        guard let mostRecentLocation = locations.last else { return }
        let place = mostRecentLocation.coordinate
        print("\(place)")
        manager.stopUpdatingLocation()
    }


    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("InterfaceController: \(NSDate())  - locationManager didFailWithError")
        print("CL failed: \(error)")
    }
}

来源:https://stackoverflow.com/questions/33594082/watchkit-corelocation-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!