一段时间后通知后台下载

时间:2014-01-09 11:48:24

标签: ios iphone nsnotificationcenter

在我的应用中,我需要在一段时间后更新数据,比如5天,用户使用应用程序5天后,下载视图就会完成任务。 我希望在用户使用应用程序5天后安排一些通知来触发下载功能。我可以自定义检查时间,但我不想对每个applicationDidBecomeActive执行检查,看看是否已经过了5天。这样做有没有其他干净的方式。我正在寻找NSNoticationCenter,但我找不到任何相关的事情。任何其他方式这样做

2 个答案:

答案 0 :(得分:3)

您可以使用具有特定fireDate的UILocalNotification。查看https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html

上的文档

答案 1 :(得分:1)

您可以使用UILocalNotification

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
localNotification.fireDate = futureDate; //Modify the date according to your logic 
localNotification.alertBody = @"Some Alert";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

您必须计算下一次下载何时发生,并在fireDate中提供该日期。

如果您的应用在后台,则会调用此方法。

application: didFinishLaunchingWithOptions:

如果它在前台,则会调用此方法

application: didReceiveLocalNotification:

在此处下载数据的条件,并在您的操作后创建另一个Localnotification并安排它

如果您不想使用本地通知,请保留NSSuserdefaults中存储的计划日期。当用户每次启动您的应用时都会检查日期和时间。然后在那种情况下做你的东西。它不需要在applicationDidBecomeActive中它可以在didFinishLaunchingWithOptions中。因此,当用户启动您的应用程序时,下载将会重新开始。

相关问题