gpt4 book ai didi

ios - 显示下载进度的通知小部件

转载 作者:行者123 更新时间:2023-11-28 21:32:59 24 4
gpt4 key购买 nike

是否可以更新今天的小部件以显示文件的下载进度?

这是我的 Today Extension 代码:

- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil];
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.preferredContentSize = CGSizeMake(320, 50);

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateProgress) name:@"UpdateWidget" object:nil];

[self updateProgress];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)userDefaultsDidChange:(NSNotification *)notification
{
[self updateProgress];
}

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.

// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData

[self updateProgress];

completionHandler(NCUpdateResultNewData);
}

- (void)updateProgress
{
double progress = 0;
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.xxxx"];
progress = [defaults doubleForKey:@"Percent"];
_percentLabel.text = [NSString stringWithFormat:@"%0.f%%", progress];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self updateProgress];
}

然后这是我在应用程序中更新用户默认值的地方:

double currentProgress = totalBytesWritten / (double)totalBytesExpectedToWrite;

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.xxxx"];
[defaults setDouble:(currentProgress*100) forKey:@"Percent"];
[defaults synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateWidget" object:nil];

我需要改变什么?这甚至可能是我想要做的吗?我也想显示进度条更新。

最佳答案

我能够使用以下方法解决此问题:

CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateProgress)];

关于ios - 显示下载进度的通知小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35112161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com