用Siri顯示二維碼, iOS11, INGetVisualCodeIntent

來源:互聯網
上載者:User

標籤:lan   object   desire   config   cts   restrict   wan   interface   creat   

再過不久,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 Foundationimport Intentsclass 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.