iOS计时器在后台运行

mji*_*cua 5 cocoa-touch background ios ios7

我的应用程序需要定期调用方法.

即使应用程序已在后台运行,也应该执行此调用.

无论何时应用程序可以从后台恢复,应用程序可以在后台运行的时间都是未定义的.

我有一个类来管理计时器:

@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
Run Code Online (Sandbox Code Playgroud)

执行:

#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
Run Code Online (Sandbox Code Playgroud)

我用TimerController这种方式使用这个类:

-(void)viewDidLoad{
[super viewDidLoad]
TimerController *timerC = [TimerController sharedInstance];
[timerC startTimerForSender:self withTimeInterval:30];

}

#pragma mark - TimerDelegate

- (void)startTimerAction:(id)userInfo
{
NSLog(@"Method called");
}
Run Code Online (Sandbox Code Playgroud)

我可以NSLog每隔30秒阅读一次条目.将应用程序置于后台使定时器停止,并且当应用程序恢复计时器正常工作时,它不会每30秒写入一次日志.

虽然应用程序在后台,但我需要每隔30秒调用一次方法.

我读到了这个问题:计时器处于后台模式

我必须能够向AppStore提交申请.

谢谢

mji*_*cua 7

- (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;
    });
}
Run Code Online (Sandbox Code Playgroud)

来源:iPhone Programmig指南

类似的问题:https://stackoverflow.com/a/8613624/1827690