简体   繁体   English

iOS 8:在后台使用计时器

[英]iOS 8: Using timer in the background

I am implementing an iOS8 app that runs in the background and sends a notification that asks a question every 10 mins for 3 hours. 我正在实现一个在后台运行的iOS8应用程序,并每3分钟发送一次通知,每10分钟询问一个问题。 I have heard that running timers in the background for 3 hours cannot be done because the app will be suspended after a couple of minutes of going the background state. 我听说无法在后台运行计时器3个小时,因为在进入后台状态几分钟后,该应用将被暂停。 Is that true? 真的吗? If not will the appstore accept such an app? 如果没有,应用商店会接受这样的应用吗?

There is no point for sending local notificatiosn for 3 hours in background. 在后台发送3小时的本地通知毫无意义。

Best option would be to prepare a logic which will will create a loop and schedule multiple local notification for 3 hours for example you can schedule n notifications for m duration. 最好的选择是准备一个逻辑,它将创建一个循环并安排3个小时的多个本地通知,例如,您可以安排m个持续时间的n个通知。 This way even your app will be suspended you will be getting local notifications and you can unregister all notification if user open the app. 这样,即使您的应用程序将被暂停,您也将收到本地通知,并且如果用户打开应用程序,则可以注销所有通知。

And regarding your second question: There is no relation with appstore policy for scheduling local notifications in background. 关于第二个问题:与在后台调度本地通知的appstore策略无关。

 NSDate *newDate = [[NSDate date] dateByAddingTimeInterval:totalDuration];

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = newDate;

[[UIApplication sharedApplication] scheduleLocalNotification:notification];

This is just a rough code that you can schedule localNotifications by creating a loop with certain timeintervals. 这只是一个粗略的代码,您可以通过创建具有一定时间间隔的循环来安排localNotifications的时间。 Just put your own logic ie 10 mins for 3 hours and supply newDate by adding time interval - 10*60secs 只需输入您自己的逻辑,即3分钟10分钟,然后通过添加时间间隔-10 * 60secs提供newDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM