【widget-iOS14-swift】widget使用自定义的Intent提供动态数据

开个坑,大概流程:添加一个Intent的配置后,写一个class继承它,然后实现一个泛型方法提供数据给Intent配置文件。

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
//
// IntentHandler.swift
// bilifansIntent
//
// Created by zl on 2020/10/11.
//

import Intents
import SwiftUI

class IntentHandler: INExtension , UpListIntentHandling{





func provideUpidOptionsCollection(for intent: UpListIntent, with completion: @escaping (INObjectCollection<Ids>?, Error?) -> Void) {

let localUpList = UtilsStore.getUpIdList()

let widgetInitList:[Ids] = localUpList.map { (id) -> Ids in
let ids = Ids(
identifier: "\(id)",
display: "UP:\(id)"
)
ids.id = NSNumber(value: id)

return ids

}

let collection = INObjectCollection(items: widgetInitList)
//传递给小组件
completion(collection, nil)

}





}

等有空在写。