需求: 

我们需要app应用刷新时间线,让桌面组件加载最新内容。即app修改共享数据后,需要通知桌面小组件强制刷新显示改变后的内容

当某种情况影响到小组件当前时间线时,您的 App 可以指示 WidgetKit 请求一个新的时间线。在上面的游戏组件示例中,如果 App 收到一条推送通知,说明队友已为角色提供了治疗药水,则 App 可以指示 WidgetKit 重新载入时间线并更新小组件的内容。为重新载入特定类型小组件,您的 App使用

WidgetCenter.shared.reloadTimelines(ofKind: "你小组件的kind")

看完此篇文章您将实现一下效果

项目配置实现

注意点:共享数据组请用自己开发者账号进行调试 

app代码

#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.5501icon-default.png?t=MBR7https://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进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注