2020年06月22日的WWDC上iOS14的新特性-小部件正式在iOS上线,同时WidgetKit也正式面向广大开发者使用。
也正是因为对Android的小部件有所了解,故想尝试下iOS的小部件的开发,并且发现当前并没有相关的文章,故记录下我学习WigetKit的经历,以下均为自己学习路上的经历,可能会有些问题,还望大佬指正。
同时已把学习路上写的代码开源 - iWiget,看完这篇文章认为有用就点个Star呗!
项目地址: https://github.com/Littleor/iWidget
开发Widget需要使用到Xcode12,目前依旧是beta版本,需要在官网使用开发者账号下载。
开发者账号的注册是免费的,使用AppleId注册后直接下载使用即可。
具体步骤网上有很多,这里不再赘述。
欲使用WidgetKit必先创建一个iOS的项目,按常规操作来即可。
点击Create a new Xcode Project来创建新项目
选择iOS->App再点击Next
Name随便填,Organization Identifier填写翻转域名即可。
创建完成就出现默认的Hello World!啦
首先点击左上角的File->New->Target添加Target 然后选择Widget Extension 配置Wiget即可,Name和项目名字不能一样哦 点击Finsh之后Xcode就会帮你自动生成一个默认的Widget文件了 运行到iOS14设备上后就可以看到默认的时钟Widget了!
Provider实现了IntentTimelineProvider,主要用于提供数据和控制数据的刷新,其中有两个关键函数:public func snapshot(for configuration: ConfigurationIntent, with context: Context, completion: @escaping (SimpleEntry) -> ())和 public func timeline(for configuration: ConfigurationIntent, with context: Context, completion: @escaping (Timeline<Entry>) -> ()),其中snapshot会在Widget被添加的时候执行,timeline通过Timeline(entries: entries, policy: .atEnd)刷新数据和控制下一步刷新时间.
struct Provider: IntentTimelineProvider { public func snapshot(for configuration: ConfigurationIntent, with context: Context, completion: @escaping (SimpleEntry) -> ()) { let entry = SimpleEntry(date: Date(), configuration: configuration) completion(entry) } public func timeline(for configuration: ConfigurationIntent, with context: Context, completion: @escaping (Timeline<Entry>) -> ()) { var entries: [SimpleEntry] = [] // Generate a timeline consisting of five entries an hour apart, starting from the current date. let currentDate = Date() for hourOffset in 0 ..< 5 { let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! let entry = SimpleEntry(date: entryDate, configuration: configuration) entries.append(entry) } let timeline = Timeline(entries: entries, policy: .atEnd) completion(timeline) } }这里只写如何使用,具体内容下一篇细述。
SimpleEntry实现了TimelineEntry,主要用于保存Widget的数据。
struct SimpleEntry: TimelineEntry { public let date: Date public let configuration: ConfigurationIntent }PlaceholderView用于显示默认Widget,当Widget还没获取到数据的时候会默认显示这里的布局。
struct PlaceholderView : View { var body: some View { Text("Placeholder View") } }WidgetDemoEntryView是Widget的布局部分,是Widget的View的部分。
struct WidgetDemoEntryView : View { var entry: Provider.Entry var body: some View { Text(entry.date, style: .time) } }这里可以说是Widget的入口了罢,这里定义了Widget的Kind、Provider、View等。
@main struct WidgetDemo: Widget { private let kind: String = "WidgetDemo" public var body: some WidgetConfiguration { IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider(), placeholder: PlaceholderView()) { entry in WidgetDemoEntryView(entry: entry) } .configurationDisplayName("My Widget") .description("This is an example widget.") } }iOS创建多个小部件不能像Android一样直接建立多个Widget的配置文件,否则会报错,而是可以直接修改当前Widget的入口文件即可,Apple提供了相关API:
@main struct Widgets: WidgetBundle { @WidgetBundleBuilder var body: some Widget { Widget1() Widget2() Widget3() } }改变@main为WidgetBundle,再创建多个Widget的struct即可.
在这几天对WidgetKit的学习后,从开发者的角度来说,iOS的Widget开发实在让人舒适,而没有Android Widget开发的那种凌乱感(也可能是我太菜了才感觉凌乱…)
总体对iOS的Widget还是很满意的,无论是开发体验还是用户体验(除了iOS14 Public Beta在点击配置小部件的时候会卡一下)。
后续还会慢慢完善WidgetKit开发的文章,同时iWiget也会不断完善,这篇文章对你有用就点个Star吧!
项目地址: https://github.com/Littleor/iWidget