swift 如何使用AppIntent动态打开应用

ddarikpa  于 9个月前  发布在  Swift
关注(0)|答案(1)|浏览(204)

我正在尝试动态确定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(),因为它们需要用户确认。

lokaqttq

lokaqttq1#

您遇到的错误是由trying to return different types as some Type .引起的
这与@ViewBuilder在SwiftUI中通过将结果 Package 在单个类型中来处理的事情是一样的。然而,由于这涉及静态属性, Package 器类型(我相信)在这里不能工作。
因此,似乎您无法根据条件调用不同的Intent类型。This discussion would also indicate what you are asking for is impossible.
现在你当然可以,链意图。但据我所知,打破这个链的唯一方法是抛出一个错误。这给了我们一个令人难以置信的黑客解决方案:

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())
    }
}

字符串
……所以你可能应该用不同的方式标注意图。

相关问题