首页 技术 正文
技术 2022年11月13日
0 收藏 628 点赞 2,639 浏览 9318 个字

下载 Xcode 8,配置 iOS 10 和 Swift 3

(可选)通过命令行编译

除 非你想使用命令行编译,使用 Swift 3.0 的工具链并不需要对项目做任何改变。如果你想的话,打开 Xcode-beta,然后从顶部菜单栏中选择 Xcode > Preferences,接着选择 Location,在页面的底部,你会看到「Command Line Tool」这行设置,请在这里选择 Xcode 8.0。

现在,在 Terminal 使用命令行找到工程所在的文件夹,调用 xcodebuild 命令就可以编译工程了。

(可选)移植现有的 Swift 2 应用

如 果你想对一个已使用 Swift 2.0 开发的工程引入 Siri 功能,需要点击工程,选择 Build Settings,在 Swift Compiler – Version 下面,找到 Use Legacy Swift Language Version 选项,设置成 No。这会造成编译器报错,然后你可以根据这些报错信息来修改代码,推荐你使用这个设置来更新代码,以适应 Swift 不断进化的语义。

开始使用 SiriKit

首先,在你的 App(或者是新建一个单视图的 Swift 模板工程),点击顶部的工程,然后点击左侧下方的 + 按钮,在这里(译者注:我在这里添加了一张图片,能够说的更明白)点击。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

弹出的窗口中,选择 iOS > Application Extension,接着选择 Intents Extension。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

这 样就给工程添加了一个新的 intent,用于监听 Siri 的命令。其中的 Product Name 应该和你的工程文件名字相似,比如,你的 App 名为 MusicMatcher,你可以把这个 intent 的名字命名为 MusicMatcherSiriIntent。一定要选中 Include UI Extension 选项,我们之后会用到,这也是添加额外扩展的最简单的方法。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

我 刚刚创建的两个新 target 可以从项目的文件层级上找到。找到 Intent 文件夹下的 IntentHandler.swift 文件,看一下这里面的样本代码。默认会提供一些示例代码,允许用户说一下诸如「用 MusicMatcher 开始锻炼」的命令,MusicMatcher 是 App 的名字。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

像这样运行示例应用

这个时候最好编译一下代码,然后在 iOS 真机上试一下命令。继续,编译应用的 target,从 Scheme 下拉菜单里选择 MusicMatcher,然后选择真机,点击 Run。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

你看你会看到一个空白的应用出现,你使用的扩展这时会在后台加载到设备的系统文件里,现在点击 Stop 按钮来关闭应用。

接下来,找到你的 scheme,选择 Intent target,点击 Run。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

这时会出现一个弹出框,问你需要连接哪个应用,选择你刚刚运行的应用:MusicMatcher。这会让真机上再次出现这个应用(还是一个空白的应用),不过这次调试台(debugger)中会出现连接的 Intent 扩展。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

现在点击 home 按钮回到首屏,或者应用可能自己就退出了,因为你正在运行的是 Intent,不是应用本身(这不是崩溃!!!)。

启用扩展

扩展都已安装就位了,但是作为一个 iOS 用户,仍然需要进行 Siri 设置才能使用扩展。点击测试设备里的 Settings,选择 Siri 菜单,你会看到 MusicMatcher 出现在清单里,激活允许使用 Siri。

测试我们第一个 Siri 命令

尝试一下 Siri 命令,长按 Home 键或者说出「Hey Siri」来激活 Siri(当然需要你已经激活「Hey Siri」功能)。

试一下命令,比如「使用 MusicMatcher 开始锻炼」。

「对不起,你需要在应用里继续。」

如果你像我一样遇到了这样的错误信息:「Sorry, you’ll need to continue in the app.」(不知道什么原因,偶尔会出现这么一个问题,什么鬼?)

在控制台中你可能会看到类似的信息:

1234 dyld: Library not loaded: @rpath/libswiftCoreLocation.dylib  Referenced from: /private/var/containers/Bundle/Application/CC815FA3-EB04-4322-B2BB-8E3F960681A0/LockScreenWidgets.app/PlugIns/JQIntentWithUI.appex/JQIntentWithUI  Reason: image not foundProgram ended with exit code: 1

我们还需要在工程里添加 CoreLocation 库,确保能添加到我们编译过的 Swift 工程中。

再 次选择工程根目录,选择 MusicMatcher target。在 General 底下找到 Linked Frameworks and Libraries。点击 + 按钮,添加 CoreLocation.framework。现在可以再次编译在真机上运行,接着照着上面相同的步骤再次编译运行 intent target。

最后,从手机桌面激活 Siri。

「Hey Siri!」

「Start my workout using MusicMatcher(使用 MusicMatcher 开始锻炼)」

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

Siri 这时候应该会回应:「OK. exercise started on MusicMatcher(OK,开始用 MusicMatcher 锻炼身体)」,然后会出现一个 UI 界面写着「Workout Started(锻炼开始)」。

它是如何工作的呢?

模板中的 IntentHandler 类使用了一长串的协议:

首先最主要的就是 INExtension,允许我们一开始就把类当作一个 intent extension 来用。剩下的协议都是 intent handler 类型,在类里能够回调:

12345 INStartWorkoutIntentHandlingINPauseWorkoutIntentHandlingINResumeWorkoutIntentHandlingINCancelWorkoutIntentHandlingINEndWorkoutIntentHandling

第一个就是我们刚刚测试过的,INStartWorkoutIntentHandling。

按住 Command 键点击这些协议的名字,会看到苹果提供的文档:

12345 /*! @brief Protocol to declare support for handling an INStartWorkoutIntent @abstract By implementing this protocol, a class can provide logic for resolving, confirming and handling the intent. @discussion The minimum requirement for an implementing class is that it should be able to handle the intent. The resolution and confirmation methods are optional. The handling method is always called last, after resolving and confirming the intent. */

换句话说,这协议告诉 SiriKit 我们准备处理英文句子「Start my workout with AppName Here.」

这 会根据用户使用语言的不同而不同,不过最终的目的都是开始一次锻炼。INStartWorkoutIntentHandling 协议调用的几个方法都在示例代码里实现了。如果你想创建一个锻炼应用,你可以自行了解其他的内容。不过在这篇教程的剩余部分,我会添加一个新的 intent handler,来处理发送消息。

添加一个新的消息 Intent

确认应用可以完美运行后,让我们继续,添加一个新的 intent 类型,用于发送消息,这里的文档说明了下列信息:

Send a message

Handler:INSendMessageIntentHandling protocol

Intent:INSendMessageIntent

Response:INSendMessageIntentResponse

在类里添加 INSendMessageIntentHandling 协议。首先要明确,我们把它添加到类协议清单里,也就是在 IntentHandler.swift 文件里。由于实际上我不想使用这些 intent,所以我会删除它们,只留下这一个:

12 class IntentHandler: INExtension, INSendMessageIntentHandling {    ...

如果这时候编译,是不会通过编译的,因为我们还需要实现一些遵守 INSendMessageIntentHandling 协议所必需的方法。

另外,如果你需要核对具体是哪些方法,只需要按住 Command 键然后鼠标点击 INSendMessageIntentHandling,然后看一下哪些方法前面没有 optional 关键词即可。

在这里,我们发现只有一个必须实现的方法:

123456789 /*! @brief handling method @abstract Execute the task represented by the INSendMessageIntent that's passed in @discussion This method is called to actually execute the intent. The app must return a response for this intent. @param  sendMessageIntent The input intent @param  completion The response handling block takes a INSendMessageIntentResponse containing the details of the result of having executed the intent @see  INSendMessageIntentResponse */public func handle(sendMessage intent: INSendMessageIntent, completion: (INSendMessageIntentResponse) -> Swift.Void)

遵守新消息意图协议

回到 IntentHandler.swift 文件,添加一行分隔符(借助 jump bar,在导航查找代码时这个分隔符会非常有用)

1 // MARK: - INSendMessageIntentHandling

在 MARK 底下,我们来实现方法。我发现 Xcode 8 非常有用,通过敲击方法名字的开始部分,剩下的都能交给自动补全来完成了,然后选择对应的方法。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

在 handler 里,我们需要创建一个 INSendMessageIntentResponse,来回调闭包。先假设所有的信息发送都很成功,在 INSendMessageIntentResponse 里返回一个用户活动的成功值,和默认模板中的实现非常类似。还需要添加一个 print 方法,当 handler 方法被 Siri 事件触发后我们就能知晓啦:

123456 func handle(sendMessage intent: INSendMessageIntent, completion: (INSendMessageIntentResponse) -> Void) {    print("Message intent is being handled.")    let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent))    let response = INSendMessageIntentResponse(code: .success, userActivity: userActivity)    completion(response)}

把这个 intent 类型添加到 Info.plist

在具备处理 INSendMessageIntent 方法之前,我们需要在 Info.plist 文件里添加一些值,就当作是应用的授权吧。

在 intent 的 Info.plist 文件里,找到并点开 NSExtension 键。接着点开 NSExtensionAttributes,然后是 IntentsSupported,我们需要给 INSendMessageIntent 新添加一行,允许应用处理信息 intents。

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

测试新的 intent

现在我们已经设置好了新的 intent,来测试一下。记住,你必须先编译 App,在真机上运行,接着运行扩展进行调试,如果你不这样做,扩展要么不会工作,要么不会在 Xcode 的控制台中打印日志。调用 Siri 的 intent,你现在可以看到会出现一个新的信息窗口,这个窗口目前还是空的,毕竟我们还没有给应用编写什么逻辑,我们需要实现剩下的调用,还要添加一些信息的逻辑,实现更好的用户体验。

SiriKit Resolutions with Swift 3 and iOS 10 – SiriKit Tutorial (Part 2)

Posted on June 20, 2016 by Jameson Quave 使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程Tweet 

SiriKit Resolutions with Swift 3 in iOS 10 – SiriKit Tutorial (Part 2)

This tutorial written on June 20th, 2016 using the Xcode 8 Beta 1, and is using the Swift 3.0 toolchain.

This post is a follow-up in a multi-part SiriKit tutorial. If you have not read part 1 yet, I recommend starting there.

Resolving requests from SiriKit

In order to make our Siri integration more useful, we can help fill out the content of our message using a callback method from the INSendMessageIntentHandling protocol. Investigating this protocol you can see this show up an optional methods.

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)

So we can provide SiriKit with further information by implementing as many of these resolutions as we wish. Effectively enabling us to provide information regarding the recipients, content, group name, service name, or sender. These should be relatively self-explanatory.

Let’s try providing some static data for our title and content, to demonstrate how resolutions work.

First, let’s add the resolution for the content of the message, by implementing the resolveContent protocol method.

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

Here we create a string resolution result, and call the success function. This is the simplest way to proceed, but we also have the option of returning a disambiguation, confirmationRequired, or unsupported response. We’ll get to those later, but first let’s actually use the data Siri is providing us.

Siri will send in it’s own transcription of our message in the intent object. We’re interested in the content property, so let’s take that and embed it inside of a string.

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

The content property is an optional, and as such we need to make sure Siri actually provided a transcription. If no transcription was provided then a message won’t be entirely useful, so we need to tell Siri that the information is missing and we need this value. We can do this by returning a resolution result calling the needsValue class method on INStringResolutionResult.

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

使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

Now SiriKit knows when we try to send a message, that the content value is a requirement. We should implement the same type of thing for the recipients. In this case, recipients can have multiple values, and we can look them up in a variety of ways. If you have a messaging app, you would need to take the INPerson intent object that is passed in and try to determine which of your own user’s the message is intended for.

This goes outside the scope of this Siri tutorial, so I’ll leave it up to you to implement your own application logic for the resolveRecipients method. If you want to see an example implementation, Apple have released some sample code here.

More iOS 10 Tutorials

We’ll be continuing to investigate iOS 10 and publish more free tutorials in the future. If you want to follow along be sure to subscribe to our newsletter and follow me on Twitter.

Thanks,
Jameson

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838