首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoreLocation WidgetKit

CoreLocation WidgetKit
EN

Stack Overflow用户
提问于 2021-04-11 03:18:11
回答 1查看 122关注 0票数 0

我在让CoreLocationWidgetKit中工作时遇到麻烦。我可以在SwiftUI应用程序中使用ObservableObject类来做到这一点。现在,我不确定如何使用小部件做到这一点。

我将LocationManager添加到我的TimelineEntry中,然后在getTimeline()中调用requestLocation函数,但它也不起作用。我在我的小工具中看不到任何更新。我向我的小部件添加了正确的密钥,如here所解释的。

代码语言:javascript
复制
import Foundation
import CoreLocation
import WidgetKit

class LocationManager: NSObject, CLLocationManagerDelegate {
    
    var locationManager = CLLocationManager()
    var location: CLLocation?
    
    override init() {
        super.init()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        WidgetCenter.shared.reloadAllTimelines()
    }
    
    func requestUpdate(_ completion: @escaping () -> Void) {
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        WidgetCenter.shared.reloadAllTimelines()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else { return }
        self.location = location
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-12 15:55:52

将我的位置管理器移到TimelineProvider,并将变量添加到我的小部件TimelineEntry中。CoreLocation现在可以正确更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67038340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档