显示下载进度的通知小组件

时间:2016-01-31 09:16:38

标签: ios animation notifications widget today-extension

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

以下是今日推广的代码:

open

然后我在这里更新应用中的用户默认值:

- (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];
}

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

1 个答案:

答案 0 :(得分:2)

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

CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateProgress)];
相关问题