微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 SwiftUI 中显示空白的小部件

如何解决在 SwiftUI 中显示空白的小部件

我正在尝试在 SwiftUI 中构建一个小部件,但是在加载小部件时,它显示的只是一个空白屏幕。作为小部件和 SwiftUI 的新手,我真的不知道我哪里出错了,有人可以帮忙吗?即使只是试图在正文中打印一个简单的 Text("Hello World") 也没有任何作用。我一直在学习本教程 - https://swiftrocks.com/ios-14-widget-tutorial-mini-apps

struct GameProvider: TimelineProvider {
    typealias Entry = GameEntry

    
    func placeholder(in context: Context) -> GameEntry {
        GameEntry(date: Date(),games: nil)
    }
    
    func getSnapshot(in context: Context,completion: @escaping (GameEntry) -> Void) {
        
        let entry = GameEntry(date: Date(),games: nil)
        completion(entry)
    }
    
    func getTimeline(in context: Context,completion: @escaping (Timeline<GameEntry>) -> Void) {
        
        let currentDate = Date()
        let refreshDate = Calendar.current.date(byAdding: .minute,value: 5,to: currentDate)!
        
        self.getLiveGames { resultGames in
            if let games = resultGames {
                let entry = GameEntry(date: currentDate,games: games)
                let timeline = Timeline(entries: [entry],policy: .after(refreshDate))
                completion(timeline)
            }
        }
        
    }
    
    
    func getLiveGames(handler: @escaping (_ games: [LSGame]?) -> ()){
        let request = NSMutableuRLRequest(url: scoreSURL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
        request.httpMethod = "GET"
        request.allHTTPHeaderFields = headers
        
        URLSession.shared.dataTask(with: request as URLRequest) { (data,response,error) in
            
            guard let data = data else {return}
            
            data.decoded(as: LSGameResponse.self) { result in
                do {
                    let res = try result.get()
                    
                    guard let games = res.results else {
                        handler(nil)
                        return
                    }
                    
                    if games.count < 1 {
                        handler(nil)
                    }

                    
                    handler(games)
                    
                    
                } catch {
                    handler(nil)
                }
            }
        }.resume()
    }

    
    func getFavoriteTeams() -> [String] {
        if let userDefaults = UserDefaults(suiteName: "group.com.uSTADIUM.uSTADIUM1"){
            let teams = userDefaults.array(forKey: "favoriteTeams") as! [String]
            return teams
        }
        return []
    }

    
    
}

struct GameEntry: TimelineEntry {
    public let date: Date
    public let games: [LSGame]?
}

struct scores_WidgetEntryView : View {
    var entry: GameProvider.Entry
    
    
    
    var body: some View {
        
        if let games = entry.games {
            vstack {
                ForEach(0..<games.count) { index in
                    Text("Hello")
                }
            }
        } else {
            Text("No games!")
        }
    }
    
    
}


@main
struct scores_Widget: Widget {
    let kind: String = "scores_Widget"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind,provider: GameProvider()) { entry in
            scores_WidgetEntryView(entry: entry)
        }
        .configurationdisplayName("My Widget")
        .description("This is an example widget.")
        .supportedFamilies([.systemLarge])
    }
}

struct scores_Widget_Previews: PreviewProvider {
    static var previews: some View {
        scores_WidgetEntryView(entry: GameEntry(date: Date(),games: nil))
            .previewContext(WidgetPreviewContext(family: .systemLarge))
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。