【问题标题】:iOS Timer run in backgroundiOS 定时器在后台运行
【发布时间】:2013-12-04 10:32:51
【问题描述】:

我的应用程序需要定期调用一个方法。

即使应用程序已被置于后台,也应该执行此调用。

应用程序可以在后台运行的时间是不确定的,任何时候应用程序都可以从后台运行到恢复。

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

@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");
}

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

尽管应用程序在后台,但我需要每 30 秒调用一次方法。

我读到了这个问题:Timer in background mode

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

谢谢

【问题讨论】:

  • 您无法决定代码何时在后台运行。 iOS 7 将允许您定期刷新内容,但这并没有保证间隔

标签: ios cocoa-touch background ios7


【解决方案1】:
- (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;
    });
}

来源:iPhone Programmig Guide

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

【讨论】:

  • 这会给你 3 分钟的后台时间。
【解决方案2】:

要在后台连续运行您的应用程序,您需要在 plist 中为您的应用程序注册所需的后台模式

Documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多