Mey*_*sam 8 swiftui appintents
我目前正在创建一个App
应该通过 AppIntent 启动的项目。例如,每当我打开Instagram时,它都应该触发 AppIntent,然后由 AppIntent 决定我的应用程序是否应该打开。第一部分已经可以通过 Apple Shortcuts 正常工作。问题是动态打开或不打开我的App
. 不幸的是 openAppWhenRun 字段应该是静态的并且不能在运行时计算。
这是到目前为止的 AppIntent:
struct OpenTimeEfficientIntent: AppIntent {
static var title: LocalizedStringResource = "Opens the app conditionally"
static var openAppWhenRun: Bool = false
@Parameter(title: "OpenedApp", optionsProvider: Provider())
var app: String //this is the initially opened app e.g. Instagram (i want to pass it)
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
//at this point I want to decide, whether the app should be launched or not
}
}
Run Code Online (Sandbox Code Playgroud)
只是要说明:我不想决定要打开哪个视图,而是决定是否要打开视图App
。
我已经尝试通过 Deeplinks 打开应用程序,这在 iOS 上是不允许的,因为 AppIntent 只是一个后台任务:The App is neither visible nor entitled, so may not perform un-trusted user actions
尝试动态设置openAppWhenRun
也不起作用。
该应用程序基本上是一个确认对话框。当我按“否”时,应用程序将关闭并且没有任何反应。当我按“是”时,Instagram 将再次打开。但在这种情况下,我不想再次启动我的应用程序,以防止陷入循环。
如果您采用ForegroundContinuableIntent
而不是AppIntent
,它将在后台运行,直到您指示它需要在函数中打开您的应用程序perform
。这使您可以有条件地决定是否要打开您的应用程序。
@available(iOS 16.4, *)
struct OpenTimeEfficientIntent: ForegroundContinuableIntent {
static var title: LocalizedStringResource = "Opens the app conditionally"
static var openAppWhenRun: Bool = false
@Parameter(title: "OpenedApp", optionsProvider: Provider())
var app: String //this is the initially opened app e.g. Instagram (i want to pass it)
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
//at this point you can decide whether the app should be brought to the foreground or not
// Stop performing the app intent and ask the user to continue to open the app in the foreground
throw needsToContinueInForegroundError()
// You can customize the dialog and/or provide a closure to do something in your app after it's opened
throw needsToContinueInForegroundError("Please continue to open the app.") {
UIApplication.shared.open(URL(string: "yourapp://deeplinktocontent")!)
}
// Or you could ask the user to continue performing the intent in the foreground - if they cancel the intent stops, if they continue the intent execution resumes with the app open
// This API also accepts an optional dialog and continuation closure
try await requestToContinueInForeground()
return .result(dialog: "I opened the app.")
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1481 次 |
最近记录: |