iOS Widget 05 - Making a Dynamic Configurable Widget

Demo-05 Download.zip Give users the option to customize their widgets by adding a custom Intent definition to your project. (Developer Documentation)

If possible, please see the Article “iOS Widget 02” first.

Expect

Widget Widget
img img

add New Type

img

img

img

If you don’t need use the Siri, please don’t select the CheckBox.

add Target: Intents Extension

img

img

img

img

Target Membership

We need add the Target Membership for the files.

img

img

Implementation

img

Please remember the name of the Intents (the Number - “2”), in my Project the first Intent’s name is “Configuration”

and the Parameter “exchange” (the Number - “4”)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import Intents

class IntentHandler: INExtension, ConfigurationIntentHandling {
    
    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
    }
    
    // provide [Parameter] OptionsCollection    // in the Intentdefinition
    func provideExchangeOptionsCollection(for intent: ConfigurationIntent, with completion: @escaping (INObjectCollection<CustomExchange>?, Error?) -> Void) {
        // Create a collection with the array of characters.
        let collection = INObjectCollection(items: IntentHandler.exchanges)
        // Call the completion handler, passing the collection.
        completion(collection, nil)
    }
    
    public static var exchanges: [CustomExchange] {
        [
            CustomExchange(identifier: "CNY", display: "人民币"),
            CustomExchange(identifier: "USD", display: "美元"),
            CustomExchange(identifier: "GBP", display: "英镑"),
            CustomExchange(identifier: "EUR", display: "欧元"),
            CustomExchange(identifier: "CAD", display: "加拿大元"),
            CustomExchange(identifier: "HKD", display: "港元"),
            CustomExchange(identifier: "MOP", display: "澳门元"),
            CustomExchange(identifier: "THB", display: "泰铢"),
            CustomExchange(identifier: "SGD", display: "新加坡元"),
        ]
    }
}

Result

Widget Widget
img img
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy