iOS应用在后台运行 [英] iOS app running in the background

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

问题描述

我正在尝试设置我的应用在后台保留多长时间。我希望能够控制我的应用程序在后台保持活动状态的时间,直到它关闭为止。有没有我可以添加到Info.plist中的东西,可以设置它,使用bool开关让它在后台运行?

I am trying to be able to set how long my app will remain in the background. I want to be able to have the control over how long my app can stay active in the background until it is closed. Is there something I can add to the Info.plist that can set that, with the bool that is the switch to have it run in the background or not?

我有阅读并发现Apple不允许应用程序在后台停留超过10分钟,但我已经做了一些测试,发现其他类似于我的实时应用,持续时间超过10分钟。我还发现旧的帖子通过在10分钟之前设置和重置定时器来绕过这个,这是否仍适用于iOS7并且Apple仍然接受它吗?

I have read and found that Apple doesn't let apps remain in the background for longer than 10min, but I have done some testing and found other live apps that are similar to mine, last longer than 10min. I have also found old posts that bypass this by setting and reseting timers before the 10min mark, does this still work with iOS7 and does Apple still accept it?

推荐答案

编辑:误解了问题。这是一个可能的替代方案。

Misunderstood the questions. Here is a possible alternative.


- (void)applicationDidEnterBackground:(UIApplication *)application{
      UIApplication*    app = [UIApplication sharedApplication];
      task = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:task];
        task = 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.
        NSLog(@"Started background task timeremaining = %f", [app backgroundTimeRemaining]);
        // save time to NSUserdefaults
        [[NSUserdefaults standardUserDefaults] setObject:[NSDate date] objectForKey@"startDate"];

        [app endBackgroundTask:task];
        task = UIBackgroundTaskInvalid;
    });

}

- (void)applicationWillTerminate:(UIApplication *)application{
    [[NSUserdefaults standardUserDefaults] setObject:[NSDate date] objectForKey@"terminateDate"];
}

-(void)applicationDidFinishLaunching:(UIApplication)application{
   //get start time & terminate time & then take a diffrence
   NSDate* temporaryDate1 = (NSDate*)[userDefaults objectForKey:@"startDate"];
   NSDate* temporaryDate2 = (NSDate*)[userDefaults objectForKey:@"terminateDate"];
   compare[temporaryDate1,temporaryDate2];
}

这是 link 以检查如何比较NSDate实例。

Here is a link to check how to compare NSDate instance.

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

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