iOS Widget 06 - Making a Dynamic Configurable Widget for tow Intent

iOS Widget

Demo-06 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 05” first.

Preview

Widget’s Preview with select.

Widget -
img img

img

img

Code

 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Intents

class IntentHandler: INExtension, ConfigurationIntentHandling, CustomConfigurationIntentHandling {
    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
    }
    
    /// CustomConfigurationIntentHandling
    // provide [Parameter] OptionsCollection    // in the CustomConfigurationIntent
    func provideSecondTypeOptionsCollection(for intent: CustomConfigurationIntent, with completion: @escaping (INObjectCollection<SecondExchange>?, Error?) -> Void) {
        // Create a collection with the array of characters.
        let collection = INObjectCollection(items: IntentHandler.secondTypes)
        // Call the completion handler, passing the collection.
        completion(collection, nil)
    }
    
    /// ConfigurationIntentHandling
    // provide [Parameter] OptionsCollection    // in the ConfigurationIntent
    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: "新加坡元"),
        ]
    }
    
    public static var secondTypes: [SecondExchange] {
        [
            SecondExchange(identifier: "CNY", display: "Yuan"),
            SecondExchange(identifier: "USD", display: "Dollor"),
            SecondExchange(identifier: "GBP", display: "Pound"),
            SecondExchange(identifier: "EUR", display: "Euro"),
        ]
    }
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy