LoginSignup
3
3

More than 3 years have passed since last update.

iOS14のウィジェットのテンプレートをIntentConfigurationで作る。

Last updated at Posted at 2020-10-19

iOS14で追加になったウィジェットのテンプレートをIntentConfigurationで作ります。
("ウィジェットを編集"メニューが表示される方)
できるだけXcodeの自動生成を使います。
テンプレートなので何の機能もありませんが、ウィジェットを量産するときの基礎になることを期待しています。

Appの作成

まだUIKitで作っているアプリがほとんどだと思うので、UIKit App Delegate を選択しています。
名前はクレジットカードアプリを想定してLeaderCardにしました。:relaxed:
image.png

Widget Extensionの作成

ここの+を押す。
image.png

Widget Extensionを選択してNextを押す。
image.png

Include Configuration Intentにチェックを入れる。(←ここ大事!)
チェックを入れないとStaticConfigurationになります。
Widget名はLeaderCardWidgetにしました。
image.png

Intent Handlerの作成

ここの+を押す。
image.png

Intents Extensionを選択してNextを押す。
image.png

Include UI Extensionをチェックしない。(←ここ大事!)
Starting PointNoneを選択。(←ここ大事!)
Intent Handler名はLeaderCardIntentにしました。
image.png

intentdefinitionの設定

LeaderCardWidgetフォルダーの中に生成されているLeaderCardWidget.intentdefinisionを開く。
image.png
下方にある+を押してNew Typeを選択。
image.png
タイプ名にCardを設定する。Cardの中身は触らなくても良いです。
image.png

Parameters

下図のように設定する。
image.png

Parameter

Parameterは小文字でcardと設定する。(←ここ大事!)
image.png
自動生成されるクラスConfigurationIntentのプロパティ名になっているからです。

Display Name

ここで指定した文字列は下図の場所で表示されます。

Type

Cardを選択する。

Configurable

チェックする。

Dynamic Options

チェックする。

Prompt Label

ここで指定した文字列は下図の場所で表示されます。

Target Membership

LeaderCardWidgetExtensionLeaderCardIntentにチェックする。(←ここ大事!)
image.png

Intent Handlerを最低限コーディングする

初期状態

IntentHandler.swift
class IntentHandler: INExtension {

    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.

        return self
    }

}

ConfigurationIntentHandlingプロトコルを追加する。
image.png
そうすると、protocol stubを追加するか聞かれるのでFixを押す。
image.png
provideCardOptionsCollectionメソッドが追加される。
image.png

codeの部分にcompletion(nil, nil)を書く。
(テンプレートなので何も機能しないです。)

IntentHandler.swift
class IntentHandler: INExtension {
    func provideCardOptionsCollection(for intent: ConfigurationIntent, with completion: @escaping (INObjectCollection<Card>?, Error?) -> Void) {
        completion(nil, nil)
    }

    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.

        return self
    }

}

完成

これでテンプレートが完成したはずです。
実行すると以下のような画面が表示されます。
これに肉付けして目的のウィジェットに近づけていくことになります。
image.png


GitHub

こちらにTemplate repositoryを作っておりますので、よろしければご利用ください。
iOS14-Widget-IntentConfiguration-template

開発環境

  • Xcode 12.1
  • iOS 14.0 - 14.1
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3