iOS计时器在后台运行 [英] iOS Timer run in background
问题描述
我的应用程序需要定期调用方法。
My application requires calling a method periodically.
即使应用程序已经放在后台,也应该进行此调用。
This call should be done even if the application has been put in the background.
应用程序可以在后台运行的时间是未定义的,只要应用程序可以在后台运行就可以恢复。
The time that the application can be in the background is undefined, any time the application can go from being in the background to resume.
I有一个类来管理计时器:
I have a class to manage timer:
@protocol TimerDelegate <NSObject>
- (void)startTimerAction:(id)userInfo;
@end
@interface TimerController : NSObject{
NSTimer *repeatingTimer;
id <TimerDelegate> delegate;
}
+ (TimerController *)sharedInstance;
- (void)startTimerForSender:(id <TimerDelegate>)sender withTimeInterval:(NSTimeInterval)time;
- (void)stopTimer;
@end
实施:
#import "TimerController.h"
TimerController *singletonInstance;
@implementation TimerController
#pragma mark - Singleton
+ (TimerController *)sharedInstance {
if (singletonInstance == nil) {
singletonInstance = [[super allocWithZone:NULL] init];
}
return singletonInstance;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedInstance];
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
#pragma mark - Public
- (void)startTimerForSender:(id <TimerDelegate>)sender withTimeInterval:(NSTimeInterval)time
{
if (repeatingTimer) {
[self stopTimer];
}
if (sender) {
delegate = sender;
}
if (time > 0.0) {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(targetMethod:)
userInfo:[self userInfo] repeats:YES];
repeatingTimer = timer;
}
}
- (void)stopTimer
{
if (repeatingTimer) {
[repeatingTimer invalidate];
repeatingTimer = nil;
}
}
- (NSDictionary *)userInfo
{
return @{@"StartDate" : [NSDate date]};
}
- (void)targetMethod:(NSTimer*)theTimer
{
if (delegate && [delegate respondsToSelector:@selector(startTimerAction:)]) {
[delegate startTimerAction:[repeatingTimer userInfo]];
}
}
@end
我以这种方式使用 TimerController
类:
-(void)viewDidLoad{
[super viewDidLoad]
TimerController *timerC = [TimerController sharedInstance];
[timerC startTimerForSender:self withTimeInterval:30];
}
#pragma mark - TimerDelegate
- (void)startTimerAction:(id)userInfo
{
NSLog(@"Method called");
}
我可以读入 NSLog
每30秒输入一次。将应用程序置于后台使定时器停止并且每30秒不会写入日志,此时应用程序恢复计时器正常工作。
I can read in NSLog
entries every 30 seconds. Put the application in the background makes the timer stops and it do not write logs every 30 seconds, when the application resume the timer works correctly.
我需要每次调用方法30秒虽然应用程序在后台。
I need to call method every 30 seconds although the application is in the background.
我读到了这个问题:后台模式中的计时器
我必须能够向AppStore提交申请。
I must be able to submit application to AppStore.
谢谢
推荐答案
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
类似的问题: https://stackoverflow.com/a/8613624/1827690
这篇关于iOS计时器在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!