正在iOS 14小工具中获取当前位置



有人试图在iOS 14 Widget中更新用户的位置吗?在阅读了苹果开发者论坛之后,我想出了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((时,授权状态为authorizedWhenUse,但从未调用委托的方法。我错过了什么?

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

<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, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
widgetLocationManager.fetchLocation(handler: { location in
print(location)
.......
})
}

确保在小部件Info.plist文件中设置了这两个plist值:

<key>NSWidgetWantsLocation</key>
<true/>
<key>NSLocationUsageDescription</key>
<string>Put some text here</string>

相关内容

  • 没有找到相关文章

最新更新