admin管理员组文章数量:1362745
I'm trying to integrate an interactive iOS Widget written in Swift with my Flutter app, and I'm running into issues with interactive AppIntents in my widget. My goal is to have a button in the widget that triggers an AppIntent, writes a value into Shared UserDefaults (using our app group), and then my Flutter app picks up that change.
I set my widget's deployment target to iOS 18 and I'm using Xcode 15, so I should have access to the new interactive widget APIs (like .appIntent or .buttonAction(intent:)). However, when I try to use these modifiers in my widget code, I get errors such as:
“.appIntent is not available in context” “Value of type 'Button' has no member 'appIntent'” I’ve tried cleaning the Derived Data folder, verified that the widget code is only in the widget target, and double-checked that the deployment target is correctly set for all relevant targets. Despite that, the interactive modifiers don’t work as expected.
Here's a minimal code snippet of my widget:
import WidgetKit
import SwiftUI
import AppIntents
struct SimpleAppIntent: AppIntent {
static var title: LocalizedStringResource = "Simple Action"
func perform() async throws -> some IntentResult {
let userDefaults = UserDefaults(suiteName: "group.NSL_homescreenapp")
userDefaults?.set("AppIntent Triggered", forKey: "flutter_action")
WidgetCenter.shared.reloadTimelines(ofKind: "MyHomeWidget")
return .result()
}
}
struct MyHomeWidgetEntryView: View {
var body: some View {
if #available(iOS 17.0, *) {
Button("Hard ios17") { }
.buttonAction(intent: SimpleAppIntent())
.buttonStyle(.bordered)
.frame(maxWidth: .infinity)
} else {
Button("Hard ios irgendwas") { }
.buttonStyle(.bordered)
.frame(maxWidth: .infinity)
}
}
}
@main
struct MyHomeWidget: Widget {
let kind: String = "MyHomeWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { _ in
MyHomeWidgetEntryView()
}
.configurationDisplayName("My Home Widget")
.description("An interactive widget example using AppIntents.")
}
}
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), message: "Placeholder")
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> Void) {
let entry = SimpleEntry(date: Date(), message: "Snapshot")
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> Void) {
let entry = SimpleEntry(date: Date(), message: "Live Data")
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let message: String
}
which looks like that:
I’m confused because I’ve set everything as required: my widget target’s deployment target is iOS 18, I'm on Xcode 16.2, and I have imported AppIntents. Has anyone encountered similar issues with interactive widgets and AppIntents? Any ideas or workarounds would be appreciated!
本文标签: swiftInteractive AppIntents in iOS Widget Not Working on Xcode 16iOS 18Stack Overflow
版权声明:本文标题:swift - Interactive AppIntents in iOS Widget Not Working on Xcode 16iOS 18 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743822076a2544968.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论