源码

首页 » 归档 » 源码 » 详解 SiriKit—SiriKit 教程(Part 2)-ios学习从入门到精通尽在姬长信

详解 SiriKit—SiriKit 教程(Part 2)-ios学习从入门到精通尽在姬长信

分享最热门的ios资讯

1467688836533777.jpg

本文转自Swift GG

原文:SiriKit Resolutions with Swift 3 and iOS 10 – SiriKit Tutorial (Part 2),作者:Jameson Quave,译者:Crystal Sun校对:numbbbbb;定稿:CMB

前言

这篇教程写于 2016 年 6 月 20 日,使用 Xcode 8 Beta 1 和 Swift 3 开发。

此文章是 SiriKit 教程系列的第二篇,建议先去阅读第一篇

正文

处理 SiriKit 请求

为了让集成的 Siri 更有用,可以使用 INSendMessageIntentHandling 协议的回调要领增加信息内容。协议有以下可选要领:

resolveRecipients(forSendMessage intent: INSendMessageIntent, with completion: ([INPersonResolutionResult]) -> Swift.Void)
resolveContent(forSendMessage intent: INSendMessageIntent, with completion: (INStringResolutionResult) -> Swift.Void)
resolveGroupName(forSendMessage intent: INSendMessageIntent, with completion: (INStringResolutionResult) -> Swift.Void)
resolveServiceName(forSendMessage intent: INSendMessageIntent, with completion: (INStringResolutionResult) -> Swift.Void)
resolveSender(forSendMessage intent: INSendMessageIntent, with completion: (INPersonResolutionResult) -> Swift.Void)

只要实现这些要领,就可以给 SiriKit 提供更多信息,例如接收者(recipients)、内容(content)、小组名字(group name)、服务名字(service name)或者发送者(sender)。

本例的标题内容使用静态数据,主要是为了展示如何使用上面的要领。

首先实现 resolveContent 协议要领。

func resolveContent(forSendMessage intent: INSendMessageIntent, with completion: (INStringResolutionResult) -> Void) {
    let message = "My message body!"
    let response = INStringResolutionResult.success(with: message)
    completion(response)
}

这里我们创建了一个字符串并调用 success 函数。你也可以调用 disambiguation 、confirmationRequired 或 unsupported 。后面会详细说明区别,现在先介绍如何使用 Siri 提供的数据。

Siri 传入的 intent 工具包罗文字版的消息内容。我们只对 content 属性感兴趣,把它嵌入字符串中。

func resolveContent(forSendMessage intent: INSendMessageIntent, with completion: (INStringResolutionResult) -> Void) {
    let message = "Dictated text: \(content!)"
    let response = INStringResolutionResult.success(with: message)
    completion(response)
}

内容属性是可选值,如此一来,需要确保 Siri 真的提供了文字内容。如果没有文字,那消息就没什么用,这时需要见告 Siri 当前信息缺失,需要提供内容值。可以调用 INStringResolutionResult 的 needsValue 类要领来请求值。

func resolveContent(forSendMessage intent: INSendMessageIntent, with completion: (INStringResolutionResult) -> Void) {
    if let content = intent.content {
        let message = "Dictated text: \(content)"
        let response = INStringResolutionResult.success(with: message)
        completion(response)
    }
    else {
        let response = INStringResolutionResult.needsValue()
        completion(response)
    }
}

sirikit-swift-3-resolutions-sirikit-tutorial-part-2-1.png

现在当我们尝试发送消息时,SiriKit 就明白必须要提供内容值。接收方也要实现同样的功能。在本例中,接收方可以有多个值,可以使用多种要领从中查询目标值。如果你有一个聊天应用,那可以检查传入的 INPerson 工具,判断应该发送给哪个用户。

不过这些已经超出本教程的范围,留给你自己实现吧。你可以在自己的应用中实现 resolveRecipients 要领。如果需要示例代码,可以检察苹果的示例。

用意志战胜身体的惰性!

(1)

本文由 姬長信 创作,文章地址:https://blog.isoyu.com/archives/785.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:7 月 20, 2016 at 12:00 下午

关键词:

热评文章

评论:

0 条评论,访客:0 条,博主:0 条
  1. 小兵
    小兵发布于: 

    学习了

发表回复

[必填]

我是人?

提交后请等待三秒以免造成未提交成功和重复