如何在 Intent Handler 中使用获取数据来编辑 Widget iOS 14?

How to use fetch data in Intent Handler for editing Widget iOS 14?

我目前正在使用 SwiftUI 开发应用程序。

我正在尝试让 widget 用户可以使用 IntentConfiguration

编辑一些数据

我想在 IntentHandler class 中使用从 CoreData 中获取的一些数据来编辑小部件,如下图所示。

我尝试编写一些代码,但它们不起作用...

我该如何解决我的代码?


代码如下:

IntentHandler.swift

import WidgetKit
import SwiftUI
import CoreData
import Intents

class IntentHandler: INExtension, ConfigurationIntentHandling {
    
    var moc = PersistenceController.shared.managedObjectContext

    var timerEntity_0:TimerEntity?
    var timerEntity_1:TimerEntity?
    var timerEntity_2:TimerEntity?

    init(context : NSManagedObjectContext) {
        self.moc = context

        let request = NSFetchRequest<TimerEntity>(entityName: "TimerEntity")

        do{
            let result = try moc.fetch(request)
            timerEntity_0 = result[0]
            timerEntity_1 = result[1]
            timerEntity_2 = result[2]
        }
        catch let error as NSError{
            print("Could not fetch.\(error.userInfo)")
        }
    }
   
    func provideNameOptionsCollection(for intent: ConfigurationIntent, searchTerm: String?, with completion: @escaping (INObjectCollection<NSString>?, Error?) -> Void) {
        
        let nameIdentifiers:[NSString] = [
            NSString(string: timerEntity_0?.task ?? "default"),
            NSString(string: timerEntity_1?.task ?? "default"),
            NSString(string: timerEntity_2?.task ?? "default")
            // "meeting",
            // "cooking",
            // "shoping"
        ]
        let allNameIdentifiers = INObjectCollection(items: nameIdentifiers)
        
        completion(allNameIdentifiers,nil)
    }
    
    override func handler(for intent: INIntent) -> Any {
        
        return self
    }
}

Widget.swift

import WidgetKit
import SwiftUI
import Intents

struct Provider: IntentTimelineProvider {
    
    typealias Intent = ConfigurationIntent
    
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), configuration: ConfigurationIntent(), name: "")
    }

    func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), configuration: configuration, name: "")
        completion(entry)
    }

    func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []

        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate, configuration: configuration, name: configuration.Name ?? "")
            entries.append(entry)
        }

        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
    let configuration: ConfigurationIntent
    var name:String
}

struct TimerIntentWidgetEntryView : View {
    var entry: Provider.Entry

    var body: some View {
        Text(entry.name)
            .font(.title)
        Text(entry.date, style: .time)
    }
}

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

    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
            TimerIntentWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}

tWidget.intentdefinition


Xcode: 版本 12.0.1

iOS: 14.0

生命周期:SwiftUI 应用程序

我可以使用从 CoreData 获取数据来在小部件中显示一个列表,如下面的代码:


IntentHandler.swift

import WidgetKit
import SwiftUI
import CoreData
import Intents

class IntentHandler: INExtension, ConfigurationIntentHandling {
    
    var moc = PersistenceController.shared.managedObjectContext

    var timerEntity_0:TimerEntity?
    var timerEntity_1:TimerEntity?
    var timerEntity_2:TimerEntity?

    func provideNameOptionsCollection(for intent: ConfigurationIntent, searchTerm: String?, with completion: @escaping (INObjectCollection<NSString>?, Error?) -> Void) {
        
        let request = NSFetchRequest<TimerEntity>(entityName: "TimerEntity")

        do{
            let result = try moc.fetch(request)
            timerEntity_0 = result[0]
            timerEntity_1 = result[1]
            timerEntity_2 = result[2]
        }
        catch let error as NSError{
            print("Could not fetch.\(error.userInfo)")
        }

        let nameIdentifiers:[NSString] = [
            NSString(string: timerEntity_0?.task ?? "default"),
            NSString(string: timerEntity_1?.task ?? "default"),
            NSString(string: timerEntity_2?.task ?? "default")
            // "meeting",
            // "cooking",
            // "shoping"
        ]
        let allNameIdentifiers = INObjectCollection(items: nameIdentifiers)
        
        completion(allNameIdentifiers,nil)
    }
    
    override func handler(for intent: INIntent) -> Any {
        
        return self
    }
}