在后台运行NSTimer [英] Having a NSTimer running background

查看:86
本文介绍了在后台运行NSTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了数百种有关如何使NSTimer在后台运行的解决方案.

I've seen hundreds of solutions of how to get a NSTimer to run in the background.

我知道这是有可能的,只需查看Strava和Runkepper之类的应用即可跟踪您锻炼的时间.

I know that it is possible, just look at apps like Strava and Runkepper that tracks your time when working out.

但是,这样做的最佳实践解决方案是什么?我找不到一个统一的解决方案.

But what is the best practice solution for doing so? I can't find one unison solution for this.

此外,我希望在不同的UIViewControllers中使用NSTimer.最佳做法是如何做到的?

Also, I would like the NSTimer to be used across different UIViewControllers. How is this done as a best practice?

谢谢! :)

推荐答案

您可以在应用程序中尝试使用此代码NSTimers不会在后台运行.加入苹果,但我们仅尝试3个薄荷
AppDelegate.h

You Can Try This Code in Your application NSTimers don't run in the background. acceding to apple But We Try forcefully Only 3 mint
AppDelegate.h

@property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier backgroundTaskIdentifier;
@property (nonatomic, strong) NSTimer *myTimer;
- (BOOL) isMultitaskingSupported;
- (void) timerMethod:(NSTimer *)paramSender;

AppDelegate.m

AppDelegate.m

 - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

        if ([self isMultitaskingSupported] == NO)
        {
            return;
        }
        self.myTimer =[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerMethod:) userInfo:nil
                                        repeats:YES];
        self.backgroundTaskIdentifier =[application beginBackgroundTaskWithExpirationHandler:^(void) {
            [self endBackgroundTask];
        }];
    }

实用标记-NSTimer进程

- (BOOL) isMultitaskingSupported
{
    BOOL result = NO;
    if ([[UIDevice currentDevice]
         respondsToSelector:@selector(isMultitaskingSupported)]){ result = [[UIDevice currentDevice] isMultitaskingSupported];
    }
    return result;
}


- (void) timerMethod:(NSTimer *)paramSender{

    NSTimeInterval backgroundTimeRemaining =
    [[UIApplication sharedApplication] backgroundTimeRemaining];
    if (backgroundTimeRemaining == DBL_MAX)
    {
        NSLog(@"Background Time Remaining = Undetermined");
    }
    else
    {
        NSLog(@"Background Time Remaining = %.02f Seconds",backgroundTimeRemaining);
    }
}
- (void) endBackgroundTask
{
    dispatch_queue_t mainQueue = dispatch_get_main_queue(); __weak AppDelegate *weakSelf = self;
    dispatch_async(mainQueue, ^(void) { AppDelegate *strongSelf = weakSelf; if (strongSelf != nil){
        [strongSelf.myTimer invalidate];
        [[UIApplication sharedApplication]
         endBackgroundTask:self.backgroundTaskIdentifier];
        strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    } });
}

这篇关于在后台运行NSTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆