繁体   English   中英

NSTIMER iOS SDK的问题

[英]issue With NSTIMER iOS SDK

我写道:

[NSTimer scheduledTimerWithTimeInterval:900 target:self selector:@selector(CallGetCounts) userInfo:nil repeats:YES];

这意味着我想每5分钟重复一次计时器,但是我的计时器没有重复,无法找到原因

谁能告诉我答案。

我在“ AppDelegate ”->“ - (void)applicationDidEnterBackground:(UIApplication *)application ”方法中编写了此代码。

在后台,您的代码被挂起,并且在应用程序再次出现在前台之前,不会收到任何计时器事件。

在给定的时间间隔后,您应该去安排本地通知以从后台唤醒。 但是,它们将向用户显示他必须首先接受的弹出窗口或横幅。

以下是有关操作方法的一些步骤:

// When you want to schedule:
UILocalNotification* localNotification = [[[UILocalNotification alloc] init] autorelease];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // seconds
localNotification.timeZone = [NSTimeZone defaultTimeZone];  
localNotification.alertBody = @"Body text";
localNotification.alertAction = @"Button text";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

// when it's fired it will call your AppDelegate's didReceiveLocalNotification
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification 
{ 
    // you can handle timer event here and eventually re-schedule your local notification
}

通常,当应用进入后台时,它会被挂起,因此根本不会执行。 特别是,NSTimers不会触发。 如果您希望后台发生某些事情,则需要将您的应用程序配置为在后台运行,并使用一种批准的方法来执行您想要执行的任务。 运行NSTimers不是受支持的任务之一。

建议您阅读《 iOS编程指南》,尤其是“ 后台执行和多任务处理”部分。

UILocalNotification实例会在您设置的时间触发时触发弹出框(并唤醒您的应用程序)。如果您确实选择了UILocalNotification这里有SO线程中讨论的很好的教程链接。 希望那些会帮助您。

暂无
暂无

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

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