背景
最近公司要求給項(xiàng)目添加小組件功能苔严,發(fā)現(xiàn)需求有個(gè)細(xì)節(jié):根據(jù)用戶是否添加小組件,做引導(dǎo)功能
主要內(nèi)容
尋尋覓覓孤澎,終于找到該方法
WidgetCenter.shared.getCurrentConfigurations
返回的是閉包:(Result<[WidgetInfo], Error>) -> Void)
通過(guò)遍歷數(shù)組[WidgetInfo]届氢,篩選出是否有我們想要的widget,便可以判斷是否有添加
詳見(jiàn)以下代碼
/// 判斷widget是否存在
static func isWidgetExist(complete: @escaping (_ exist: Bool) -> Void) {
if #available(iOS 14.0, *) {
WidgetCenter.shared.getCurrentConfigurations { result in
var exist = false
defer {
complete(exist)
}
guard case .success(let widgets) = result else {
return
}
let currentWidget = widgets.first( where: { widget in
print(widget.kind)
var kind = "mywidget" //widget的唯一id
return widget.kind == kind
})
if currentWidget != nil {
exist = true
}
}
} else {
complete(false)
}
}