需求:
我们需要在app应用内刷新时间线,让桌面小组件加载最新的内容。即app内修改了共享数据后,需要通知桌面小组件强制刷新,显示改变后的内容。
当某种情况影响到小组件的当前时间线时,您的 App 可以指示 WidgetKit 请求一个新的时间线。在上面的游戏小组件示例中,如果 App 收到一条推送通知,说明队友已为角色提供了治疗药水,则 App 可以指示 WidgetKit 重新载入时间线并更新小组件的内容。为重新载入特定类型的小组件,您的 App 会使用
WidgetCenter.shared.reloadTimelines(ofKind: "你小组件的kind")
项目配置和实现:
#import "ViewController.h"
#import "MyJbbDemo-Swift.h"
@interface ViewController ()
@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, assign) bool isChange;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *startButton = [UIButton buttonWithType:UIButtonTypeSystem];
startButton.frame = CGRectMake(0, 0, 200, 50);
startButton.backgroundColor = UIColor.greenColor;
startButton.layer.cornerRadius = 25;
[self.view addSubview:startButton];
[startButton setTitle:@"切换小组件上显示的图片" forState:UIControlStateNormal];
[startButton setTintColor:[UIColor whiteColor]];
startButton.center = self.view.center;
[startButton addTarget:self action:@selector(startClick:) forControlEvents:UIControlEventTouchUpInside];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *pathURL = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"你的组ID"];
pathURL = [pathURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", @"testWidgetImage"]];
UIImage *image = [UIImage imageNamed:@"1111"];
BOOL success = [UIImagePNGRepresentation(image) writeToURL:pathURL atomically:YES];
}
- (void)startClick:(UIButton *)sender {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *pathURL = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"你的组ID"];
pathURL = [pathURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", @"testWidgetImage"]];
UIImage *image = [UIImage imageNamed:@"2222"];
if(self.isChange){
image = [UIImage imageNamed:@"1111"];
}
self.isChange = !self.isChange;
BOOL success = [UIImagePNGRepresentation(image) writeToURL:pathURL atomically:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
WidgetTool *tool = [[WidgetTool alloc] init];
[tool refreshAllWidget];
[[UIApplication sharedApplication] performSelector:@selector(suspend)];
});
}
@end
因为我的项目是oc项目,所以我使用了侨接,来调用swift方法去通知小组件刷新:
import WidgetKit
@objcMembers class WidgetTool: NSObject {
@available(iOS 14, *)
@objc func refreshAllWidget() {
#if arch(arm64) || arch(i386) || arch(x86_64)
WidgetCenter.shared.reloadAllTimelines()
#endif
}}
import WidgetKit
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration)
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in 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)
var entries: [SimpleEntry] = []
let currentDate = Date()
for hourOffset in 0 ... 23 {
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: .never)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
}
struct testExtensionEntryView : View {
var entry: Provider.Entry
var body: some View {
getImage()
.resizable()
.frame(width: .infinity, height: .infinity)
.scaledToFill()
// Text("aaaaaa")
// .foregroundColor(Color.red)
}
}
struct testExtension: Widget {
let kind: String = "testExtension"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
testExtensionEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct testExtension_Previews: PreviewProvider {
static var previews: some View {
testExtensionEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
func getImage() -> Image {
let manager = FileManager.default
let floderURL:URL = manager.containerURL(forSecurityApplicationGroupIdentifier: "你的组ID")!
let str = "testWidgetImage.png"
let fileURL:URL = floderURL.appendingPathComponent(str)
do {
let data: Data = try Data.init(contentsOf: fileURL)
let image = UIImage.init(data: data)
return Image(uiImage: image!)
} catch let error{
print(error.localizedDescription)
return Image("WidgetBackground")
}
}
demo下载案例:
https://download.csdn.net/download/IT_Scratch/87389805?spm=1001.2014.3001.5501https://download.csdn.net/download/IT_Scratch/87389805?spm=1001.2014.3001.5501
原文地址:https://blog.csdn.net/IT_Scratch/article/details/128718973
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_18223.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。