如何使用AppIntent动态打开应用程序

问题描述 投票:0回答:1

我正在尝试动态确定 AppIntent 是否按照

openAppWhenRun
属性的指示打开应用程序。有时应该打开应用程序,有时则不应该。

我尝试过的

  1. 我尝试嵌套 AppIntent,以便主 AppIntent 要么运行打开应用程序的 AppIntent,要么运行不打开应用程序的 AppIntent。但是,它给了我以下错误:
    Function declares an opaque return type 'some IntentResult & OpensIntent', but the return statements in its body do not have matching underlying types
    func perform() async throws -> some IntentResult & OpensIntent {
        if (condition) {
                return .result(opensIntent: OtherIntent())
        }
            
        return .result(opensIntent: NestedIntent())
    }
  1. 我尝试使
    openAppWhenRun
    不是静态的并在运行时更改它,但这不起作用

需要注意的是,我也不想使用

needsToContinueInForegroundError()
requestToContinueInForeground()
,因为它们需要用户确认。

ios swift iphone swiftui appintents
1个回答
0
投票

您收到的错误是由于 尝试返回不同类型作为

some Type

这与 SwiftUI 中 @ViewBuilder 通过将结果包装为单一类型来处理的事情相同。但是,由于这涉及静态属性,因此包装类型(我相信)不能在这里工作。

所以看来你不能根据条件调用不同的意图类型。 此讨论也表明您的要求是不可能的。

现在您当然可以链接意图。但据我所知,打破这个链条的唯一方法是抛出错误。这给了我们一个令人难以置信的 hacky 解决方案:

enum DoneError: Error, LocalizedError {
    case done
    var errorDescription: String?  { "All done!" }
}

func perform() async throws -> some IntentResult {
    // do things...
    if condition {
        throw DoneError.done
    } else {
        return .result(opensIntent: OpeningAppIntent())
    }
}

...所以你可能应该以不同的方式标记意图。

© www.soinside.com 2019 - 2024. All rights reserved.