用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
用Siri顯示二維碼, iOS11, INGetVisualCodeIntent