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


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

//
//  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)
  
    }
  
  
  
   
  
}

等有空在写。


文章作者: 2winter
文章链接: https://2winter.com
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 2winter !