在iOS 14小部件中获取当前位置

问题描述

是否有人试图在iOS 14小部件中更新用户的位置? 在阅读了Apple Developer论坛之后,我想到了CLLocationManager周围的书写包装,并以此方式使用它:

class WidgetLocationManager: NSObject,CLLocationManagerDelegate {
    var locationManager: CLLocationManager? {
        didSet {
            self.locationManager!.delegate = self
        }
    }
    private var handler: ((CLLocation) -> Void)?
    
    func fetchLocation(handler: @escaping (CLLocation) -> Void) {
        self.handler = handler
        self.locationManager!.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
        self.handler!(locations.last!)
    }
    
    func locationManager(_ manager: CLLocationManager,didFailWithError error: Error) {
        print(error)
    }
}

并以这种方式使用它:

var widgetLocationManager = WidgetLocationManager()
    func getTimeline(for configuration: SelectPlaceIntent,in context: Context,completion: @escaping (Timeline<Entry>) -> Void) {
        if widgetLocationManager.locationManager == nil {
            widgetLocationManager.locationManager = CLLocationManager()
            widgetLocationManager.locationManager!.requestWhenInUseAuthorization()
        }
        widgetLocationManager.fetchLocation(handler: { location in
            print(location)
            .......
        })
    }

在Widget的info.plist中我也有这两个条目:

<key>NSLocationUsageDescription</key>
<string>1</string>

<key>NSWidgetWantsLocation</key>
<true/>

调用locationManager.requestLocation()时,授权状态为authorWhenWhenInUse,但是永远不会调用委托的方法。我想念什么?

解决方法

首先,我看到的明显问题是

<key>NSLocationUsageDescription</key>
<string>1</string>

NSLocationUsageDescription已过时:Apple Documentation,因此您应该改用NSLocationWhenInUseUsageDescriptionNSLocationAlwaysAndWhenInUseUsageDescription。确保同时在主应用Info.plist中包含您选择的权限

另外,在其中创建CLLocationManager

func getTimeline(for configuration: SelectPlaceIntent,in context: Context,completion: @escaping (Timeline<Entry>) -> Void) {
    ...
}

可能会出现问题,因为它可以从后台线程中调用,所以我将像这样重构您的WidgetLocationManager

class WidgetLocationManager: NSObject,CLLocationManagerDelegate {
    var locationManager: CLLocationManager? 
    private var handler: ((CLLocation) -> Void)?

    override init() {
        super.init()
        DispatchQueue.main.async {
            self.locationManager = CLLocationManager()
            self.locationManager!.delegate = self
            if self.locationManager!.authorizationStatus == .notDetermined {
                self.locationManager!.requestWhenInUseAuthorization()
            }
        }
    }
    
    func fetchLocation(handler: @escaping (CLLocation) -> Void) {
        self.handler = handler
        self.locationManager!.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
        self.handler!(locations.last!)
    }
    
    func locationManager(_ manager: CLLocationManager,didFailWithError error: Error) {
        print(error)
    }
}

,然后像这样使用它:

var widgetLocationManager = WidgetLocationManager()

func getTimeline(for configuration: SelectPlaceIntent,completion: @escaping (Timeline<Entry>) -> Void) {
    widgetLocationManager.fetchLocation(handler: { location in
        print(location)
        .......
    })
}