桌面小组件要求IOS14及以上,锁屏小组件则需要IOS16及以上。
新增小组件
1.Xcode中File->New->Target
2.勾选 “Include Configuration Intent” 复选框
Ps:小组件使用SwiftUI,所以只能使用Swift。
想要实现IOS能够每分钟刷新:
思路是:配置小组件的时间线
//在这个方法中可以进行网络请求,获得所需要的数据,并保存在对应的entry中,也就是遵循TimelineEntry协议的结构体
// 时间线刷新策略控制逻辑
func getTimeline(for configuration: TestConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [QouteEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for minuteOffset in 0..<300{
let entryDate = Calendar.current.date(byAdding: .minute, value: minuteOffset, to: currentDate)!
// let user = UserDefaults.init(suiteName: "group.com.metajoy.widget")
// var qoute = user?.string(forKey: "myKey")
// if(qoute == nil){
// qoute = "motivation"
// }
let qoute = String(describing: minuteOffset);
let entry = QouteEntry(date: entryDate, qoute: qoute, configuration: configuration)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
//当调用completion方法时,小组件的内容和界面会进行刷新。注意,小组件每日是有刷新限制的,并且只有主app在前台运行时才可以主动刷新
}
Ps:官网说活跃的app,每天有40到70次更新的资源,但是在app刚下载的几天里会有Siri学习行为,可能会超过这个次数。
手动刷新小组件的方法:
Ps:app位于前台时,刷新小组件不消耗次数资源。
import Foundation
import WidgetKit
import UIKit
@available(iOS 14, *)
@objcMembers class WidgetTool: NSObject{
class func refreshWidgetAll() {
#if arch(arm64) || arch(i386) || arch(x86_64)
WidgetCenter.shared.reloadAllTimelines()
NSLog("调用更新小部件")
#endif
}
class func refreshWidgetWithKind(kind :String) {
#if arch(arm64) || arch(i386) || arch(x86_64)
WidgetCenter.shared.reloadTimelines(ofKind: kind)
#endif
}
}
app与小组件的交互:
1.新建app Groups,为小组件和主体配置相同的app group;
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.example.widget.group"];
//group.com.example.widget.group则是配置好的app group
NSString *content = [userDefaults objectForKey:@"widget"];
[userDefaults setObject:cashStr forKey:@"widget"];
//swift获取
let sharedDefaults = UserDefaults(suiteName: "group.com.example.widget.group")
let content: String = sharedDefaults?.string(forKey: "widget") ?? ""
2.文件、图片共享
在需要共享的文件,选中相关Tag就好了,如果是OC项目的话,会自动创建桥接文件的,把相关类名放进桥接文件里面导入就好了
为小组件添加动态配置
在上面的基础上完成下面的操作:
1、选择“File”>“New”>“Target”,然后选择"Intens Extension"。
2.点击Next
3、在新Target的属性的“Gneral”选项卡中,在“Supported Intents”部分中添加一个条目,并将“Class Name”设置为 "配置文件名".
这个名字就是之前静态可配置的配置文件名
4.新建自定义类型
添加该类型的变量
5.编译一下项目,Xcode会根据xxx.intentdefinition文件生成对应的代码。
最终效果:
加入锁屏小组件:
// 小组件入口
struct test_widget: Widget {
let kind: String = "test_widget"
let re = getSupportedFamilies()
var body: some WidgetConfiguration {
// 创建时不勾选 “Include Configuration Intent”,使用 StaticConfiguration
let configuration = IntentConfiguration(kind: kind, intent: TestConfigurationIntent.self, provider: Provider()) { entry in
test_widgetEntryView(entry: entry)
}
.configurationDisplayName("My Test Widget")
.description("This is an test widget.")
if #available(iOS 16, *){
//此处只添加了1*2的小组件accessoryRectangular
return configuration.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .accessoryRectangular])
}else{
return configuration.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
}
}
}