用Siri显示二维码, iOS11, INGetVisualCodeIntent

再过不久,iOS11就要出来啦。

这次的更新中,Siri将支持显示二维码。支付啦,要求支付啦,各种要求的二维码。

听起来好像还不错,苹果想要方便支付的心意已经传达到了。

只是真的会比自己打开app显示二维码更方便快捷吗。

或者,想象一下大家都用Siri来显示支付码的画面。

便利店的队伍中此起彼伏,“我的支付码”,“快显示我的二维码”,“二维码”。。

Siri说,“二一码”? 😂

(也许是我不用Siri,场景再现全凭想象)

 

嘛,不管怎么样。可以先做个DEMO感受下。

之前没有做过支持Siri,试了一下意外得简单。

 

0. 安装Xcode 9 beta最新版和iOS11

1. 准备支持SiriKit的app ID

 填好bundle ID并勾选支持SiriKit。

 

 

2. 准备相应的provisioning profile

3. 新建或打开已有的project。设置bundle identifier和provisioning profile为前面准备的

4. 选择project的target。在Capability中启用Siri

 

5. 给project添加Intents App Extension

 

因为要显示app的二维码,Include UI Extension应该勾着。

 

然后你会发现加了两个target。

 

6. 设置这两个target的Info.plist的NSExtension

SiriQRCodeIntents的:

 

 

SiriQRCodeIntentsUI的:

 

*IntentsRestrictedWhileLocked 就是设置在锁屏状态下需要解锁才能继续。其实这里不加也可以,支付相关的Intent默认需要解锁。

 

7. 创建Handler

在SiriQRCodeIntents project下,新建一个实现INGetVisualCodeIntentHandling protocol的类。

import Foundation
import Intents

class GetVisualCodeIntentHandler: NSObject, INGetVisualCodeIntentHandling {
    func handle(intent: INGetVisualCodeIntent, completion: @escaping (INGetVisualCodeIntentResponse) -> Void) {
        completion(INGetVisualCodeIntentResponse(code: INGetVisualCodeIntentResponseCode.success, userActivity: nil))
    }
}

  

在默认的IntentHandler类的handler方法中使用。

override func handler(for intent: INIntent) -> Any {
        // This is the default implementation.  If you want different objects to handle different intents,
        // you can override this and return the handler you want for that particular intent.
        if intent is INGetVisualCodeIntent {
            return GetVisualCodeIntentHandler()
        }
        return self
}

  

 8. 跑一下看看吧

到此为止,你的app就已经成功进入“支付码”之类的命令对应的候选app名单了!

选择想debug的target,然后run! (只能真机)

选app时选择Siri。

 

 

然后给Siri发号施令吧!!“显示我的支付码”之类的?

然后如果你的app名显示出来就OK啦。

 

9. 什么都没有啊。到底怎么显示二维码??

这时就需要之前添加的SiriQRCodeIntentsUI了。

我的话,就往MainInterface.storyboard里塞了一个二维码。

跑一下发现就那么显示了,就满足啦。😎

 

10. 哦不,在那之前,调整大小。

在SiriQRCodeIntentsUI的IntentViewController里随意得设置了一下高200。

    // Prepare your view controller for the interaction to handle.
    func configureView(for parameters: Set<INParameter>, of interaction: INInteraction, interactiveBehavior: INUIInteractiveBehavior, context: INUIHostedViewContext, completion: @escaping (Bool, Set<INParameter>, CGSize) -> Void) {
        // Do configuration here, including preparing views and calculating a desired size for presentation.

        completion(true, parameters, self.desiredSize)
    }
    
    var desiredSize: CGSize {
        return CGSize(width: extensionContext!.hostedViewMinimumAllowedSize.width, height: 200)
    }

  

DEMO结果和感想

不感到快捷方便。还经常识别错。🙈

也不能排除日语口齿不清的可能性。。

所谓的对显示二维码的支持,只是支持命令部分,要显示的二维码还是需要app提供的~~

 

 

 

参考:

https://developer.apple.com/documentation/sirikit/creating_an_intents_app_extension

https://developer.apple.com/documentation/sirikit/ingetvisualcodeintent

https://developer.apple.com/documentation/sirikit/ingetvisualcodeintenthandling

 

posted @ 2017-09-09 23:54  your3i  阅读(305)  评论(0编辑  收藏  举报