仅在任务完成时才返回SiriKit Intent Response对象

时间:2019-10-03 13:45:31

标签: ios swift sirikit sirishortcuts

有没有一种方法可以异步返回我的意图响应?当我处理好自己的意图时,我正在发出网络请求,我想等待完成快捷方式流程,直到成功发出此请求。

我目前有:

func handle(intent: AppleScriptIntent, completion: @escaping (AppleScriptIntentResponse) -> Void) {
    let response = AppleScriptIntentResponse(code: .success, userActivity: nil)
    let data = intent.apple_script?.data(using: .utf8)

    let networkHandler = NetworkHandler()
    networkHandler.browseForService()
    let connection = networkHandler.connection
    connection.send(content: data, completion: .idempotent)

    completion(response)
}

但是我想要拥有:

func handle(intent: AppleScriptIntent, completion: @escaping (AppleScriptIntentResponse) -> Void) {
    let successResponse = AppleScriptIntentResponse(code: .success, userActivity: nil)
    let inProgressResponse = AppleScriptIntentResponse(code: .inProgress, userActivity: nil)
    let data = intent.apple_script?.data(using: .utf8)

    let networkHandler = NetworkHandler()
    networkHandler.browseForService()
    let connection = networkHandler.connection
    connection.send(content: data, completion: .idempotent)

    if requestSent {
        completion(successResponse)
    } else {
        completion(inProgressResponse)
    }
}

即使网络连接和请求需要5秒钟才能完成,该最后一个代码段也会运行并立即完成快捷方式。

0 个答案:

没有答案