如何解决在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,因此您应该改用NSLocationWhenInUseUsageDescription
或NSLocationAlwaysAndWhenInUseUsageDescription
。确保同时在主应用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)
.......
})
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。