由于本应用需要在应用进入后台后还要进行定时的检测功能,因此对于我来说怎样让APP在进入后台后 保持运行状态是比较大的需求。然后在iOS系统中是很难实现的,不管是 通过 音频还是 定位系统,我查找了一些资料后都是只能申请到十分钟的运行态。没有所谓的长期运行的概念。。。。。

然而在博客中突然看到这篇文章,尝试了下竟然可以运行很久。不管怎么样我还是先试试了......至于苹果是否能审核通过估计又是个难是

具体如下:

本文所讲为其中之一:iOS程序进入后台后十分钟之内就会被系统kill掉,怎么解决呢?我想要程序进入后台后仍然运行计时功能,否则就无法达到考试的目的,之后在网上查阅了相关资料最后终于找到答案,其精髓就是:利用苹果给出的三种类型的程序可以保持在后台运行:音频播放类,位置更新类,另外一个记不太清楚了,我利用了苹果给出的音频播放类的这个“特权”来满足我程序上的要求,详细步骤如下:

1、步骤一:在Info.plist中,添加"Required background modes"键,value为:App plays audio

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcAAAAAqCAYAAAAkjYsjAAAKQWlDQ1BJQ0MgUHJvZmlsZQAASA2dlndUU9kWh8+9N73QEiIgJfQaegkg0jtIFQRRiUmAUAKGhCZ2RAVGFBEpVmRUwAFHhyJjRRQLg4Ji1wnyEFDGwVFEReXdjGsJ7601896a/cdZ39nnt9fZZ+9917oAUPyCBMJ0WAGANKFYFO7rwVwSE8vE9wIYEAEOWAHA4WZmBEf4RALU/L09mZmoSMaz9u4ugGS72yy/UCZz1v9/kSI3QyQGAApF1TY8fiYX5QKUU7PFGTL/BMr0lSkyhjEyFqEJoqwi48SvbPan5iu7yZiXJuShGlnOGbw0noy7UN6aJeGjjAShXJgl4GejfAdlvVRJmgDl9yjT0/icTAAwFJlfzOcmoWyJMkUUGe6J8gIACJTEObxyDov5OWieAHimZ+SKBIlJYqYR15hp5ejIZvrxs1P5YjErlMNN4Yh4TM/0tAyOMBeAr2+WRQElWW2ZaJHtrRzt7VnW5mj5v9nfHn5T/T3IevtV8Sbsz55BjJ5Z32zsrC+9FgD2JFqbHbO+lVUAtG0GQ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3Mh1qf6FXOdiO5lavfFZckUmpUDbWMzVd7itJJXHP188mfK81wy6Y3clT4LtEZN4/r6ap3NwymEsXEFmimFxjA3r0B6b6TLWSUMgWIEtEk4GdNHU9AtJRA15XlZXMaFYKw/+UCVTeboja7vgPDjG1zwBHieR5fksJoEV4lQXpp6Rm5/LVPE/9m9bmka6vm4a1rskyyTByEJLcVsfi94Bm+qXacu/ErnVxQr/aNNAk45w2kTcfUnhROcmgMJNDMNpjUNmTn6ZVHRkL3So2qUBljpaLEKGAIMgZcgIIUoJwfPSMvU1jNBvdrqU+hLilb3ZNKwcki74bWw0m0pL61EXtKmciWvpSHxWpyaQCxB/h2/Ka0BMgH4jj8QrPkMAYYAQ+BdQaC0AKzWL8G8K53G2skQYAgwBBgCFY8A8whf8ZgyigwBhgBDgCFQBREorQEyE2gV7CTGEkOAIcAQYAhUPAKlBSAzgVY8xowiQ4AhwBBgCFQDBJgArAadxFhkCDAEGAIMgYpH4P8B30Sp1ErnQycAAAAASUVORK5CYII=" alt="" />

步骤二:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: &setCategoryErr];
[[AVAudioSession sharedInstance]
setActive: YES
error: &activationErr];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

步骤三:将以下代码添加到appDelegate文件中的- (void)applicationDidEnterBackground:(UIApplication *)application函数,也可添加到在具体类中注册的应用进入后台后的通知方法

- (void)applicationDidEnterBackground:(UIApplication *)application{

    UIApplication*   app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
});
}

完成以上步骤你会发现,程序进入后台后仍可运行定时器!

感谢 http://www.cnblogs.com/zzltjnh/archive/2013/05/15/3080058.html ! 希望我这里有用

以后也要仔细看下苹果的官方文档......

iOS程序进入后台后仍运行定时器NSTimer的更多相关文章

  1. iOS开发小技巧--iOS程序进入后台运行的实现

    iOS程序进入后台运行的实现 视频中看到老师用的iOS7,代码中有开启timer,无限请求数据的功能,但是切换到后台,代码就不打印了 自己用的iOS9,进入后台还是可以打印的,再次进入前台也可以正常运 ...

  2. iOS后台挂起程序 当程序到后台后,继续完成Long-Running Task 任务

    我们知道,到我们程序从前台退到后台(安home)键后,将执行程序的委托方法. // 当应用程序掉到后台时,执行该方法 - (void)applicationDidEnterBackground:(UI ...

  3. IOS 后台挂起程序 当程序到后台后,继续完成Long-Running Task 任务

    我们知道,到我们程序从前台退到后台(安home)键后,将执行程序的委托方法. // 当应用程序掉到后台时,执行该方法 - (void)applicationDidEnterBackground:(UI ...

  4. ios开发-程序压后台后,悄悄的抓取数据~~

    我们使用某个app的时候,当我们将程序压到后台之后,我们希望它还能从服务器抓取一些数据,类似微博,微信,qq这些程序压后台 之后,我们依然能看到icon上显示未读数量.但是ios系统是伪多任务操作系统 ...

  5. iOS 程序进入后台,包含用户上拉快捷菜单导致程序失去活跃的研究

    今日在使用某App时候,突然发现上拉菜单.程序视频扔在播放,咦!引起了我的兴趣. 首先,列出两个方法, 第一个方法是AppDelegate的代理.当程序进入后台时候调用 - (void)applica ...

  6. iOS程序进入后台,延迟指定时间退出

    程序进入后台,延迟指定时间退出 正常程序退出后,会在几秒内停止工作:要想申请更长的时间,需要用到beginBackgroundTaskWithExpirationHandlerendBackgroun ...

  7. iOS 程序切换后台

    1. -(void)animationFinished:(NSString*)animationid finished:(NSNumber*)finished context:(void*)conte ...

  8. 【Unity知识点】安卓游戏如何在切后台后继续运行

    解决方法很简单,在android项目AndroidManifest.xml文件中的activity中添加如下内容: android:configChanges="fontScale|keyb ...

  9. ios基础篇(二十三)—— 定时器NSTimer与图片的自动切换

    一.NSTimer NSTimer是一个能在从现在开始到后面的某一个时刻或者周期性的执行我们指定的方法的对象.可以按照一定的时间间隔,将制定的信息发送给目标对象.并更新某个对象的行为.你可以选择在未来 ...

随机推荐

  1. 利用Github和Hexo搭建独立的个人博客--基础篇

    利用Github和Hexo搭建独立的个人博客--基础篇 摘要:本文主要参考了使用hexo和Github上创建自己的博客.如何搭建一个独立博客--简明Github Pages与Hexo教程和使用GitH ...

  2. 5-2-2 printf参数从右往左压栈

    5-2-2 C中printf计算参数时是从右到左压栈的 #include <stdio.h> int main (int argc, char **argv) { ; ,,,,}; int ...

  3. JS预解析

    1.在逐行读js代码前,解析器会先提取所有声明的var变量和函数 js解析器会先把脚本里所有var变量声明读一遍,但是它只读变量名字,不读变量值,一开始它会赋给所有读到的var变量一个[未定义]的值. ...

  4. 20145218 《Java程序设计》第六周学习总结

    20145218 <Java程序设计>第六周学习总结 教材学习内容总结 第十章 输入/输出 10.1 InputStream与OutputStream 10.1.1 串流设计的概念 Jav ...

  5. require.js学习笔记(内容属于转载总结)

    <script data-main="src/app" src="src/lib/require.js"></script> backb ...

  6. 3.3 使用Code First数据库迁移

    当Entity Framework Code First的数据模型发生异动时,默认会引发一个System.InvalidOpertaionException异常.一种解决方法是在Global.asax ...

  7. css--block formatting context

    block formatting context(块级格式化上下文) 如何产生BFC:当一个HTML元素满足下面条件的任何一点,都可以产生Block Formatting Context: float ...

  8. hdu----(1677)Nested Dolls(DP/LIS(二维))

    Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. Sum Root to Leaf Numbers [LeetCode]

    Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...

  10. 在唯一密钥属性“name”设置为“ScriptHandlerFactory”时,无法添加类型为“add”的重复集合项

    出错原因:同一根目录下有两个相同的WebConfig文件 解决办法:删除子其中一个webConfig文件. 详细: