一、基础知识

  1) main.m指定了程序的入口点

UIApplicationMain(argc, argv,nil,NSStringFromClass([StartingPointAppDelegateclass]));

  2) AppDelegate的入口点

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}

  3) 前台和后台程序切换执行方法

- (void)applicationWillEnterForeground:(UIApplication *)application {}
程序将置于前台,程序启动的时候不会相应此方法,而是按下home以后再次回复程序的时候响应。
- (void)applicationDidBecomeActive:(UIApplication *)application {}
程序已经置于前台,程序刚启动也会响应此方法。按下home以后再次回来也会响应。
- (void)applicationWillResignActive:(UIApplication *)application {}
程序将要退出后台,按下home以后,程序不会立即消失,系统会先调用此方法。
- (void)applicationDidEnterBackground:(UIApplication *)application {}
程序已经置于后台了,按home以后,程序完全消失会调用。

二、程序后台运行

  1)在没有使用GCD时,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作。但是在使用GCD后,app最多有10分钟的时间在后台长久运行。这个时间可以用来做清理本地缓存,发送统计数据等工作。

  注意:
      当多程序切换的时候,程序不会被关闭,在系统规定的一段时间内可以正常执行。
      当一个程序使用的内存太大,而导致系统内存不足的时候,程序会被关闭,任务会终止,直到下次启动程序,并退到后台的时候,后台任务才会重新执行。
      运行的条件必须在软件不被关闭的情况下可用。

// AppDelegate.h
@property (assign, nonatomic) UIBackgroundTaskIdentifier* backgroundTaskIdentifier; // AppDelegate.m - (void)applicationDidEnterBackground:(UIApplication *)application {
[self beingBackgroundUpdateTask];
NSLog(@"加上你需要多久运行的代码");
NSLog(@"剩余执行时间: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
[self endBackgroundUpdateTask];
} -(void) beingBackgroundUpdateTask {
self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
} -(void) endBackgroundUpdateTask {
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid; }

  2) 每隔一段时间,系统自动执行的任务(设置的值必须大于等于600)

    注意:
       当多程序切换的时候,此任务也会被关闭。
         运行的条件必须在软件不被关闭的情况下可用

    执行的必要条件:
        找到程序的 plist 配置文件,增加:
        键:UIBackgroundModes 类型:Array
        在 Item0 里面设置类型:String,值:voip

[[UIApplication sharedApplication] setKeepAliveTimeout: handler:^{
//执行你想要执行的任务,同时可以配合第一种任务,以增加某些同步方法的执行时间,比如说下载数据等
}];

AppDelegate的更多相关文章

  1. 谈谈AppDelegate

    谈谈AppDelegate 前言 每个iOS程序都会有一个AppDelegate的类,这个类就是一个代理类,我们新建一个Project的时候,里面都会带有这个类.现在就让我们看看这个类. 开始介绍 对 ...

  2. cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析

    首先,是个敲代码的,基本上都知道程序的入口是main函数,显然,就算在cocos2d-x框架中也一样 我们看看main函数做了什么 #include "main.h" #inclu ...

  3. [MISS静IOS开发原创文摘]-AppDelegate存储全局变量和 NSUserDefaults standardUserDefaults 通过模型保存和读取数据,存储自定义的对象

    由于app开发的需求,需要从api接口获得json格式数据并保存临时的 app的主题颜色 和 相关url 方案有很多种: 1, 通过AppDelegate保存为全局变量,再获取 2,使用NSUSerD ...

  4. Coding源码学习第一部分(AppDelegate.m)

    前言:在此首先感谢开源,感谢大神们的无私分享. Coding 的主页:https://coding.net/app#app-feature Coding 自己家的仓库:https://coding.n ...

  5. cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西。

    cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西.

  6. 应用程序代理AppDelegate解析

    应用程序UIApplication是通过代理和外部交互的,当应用程序生命周期中发生改变时UIApplication就会调用代理对应的方法. @implementation AppDelegate - ...

  7. IOS应用程序生命周期的AppDelegate详解

    IOS 中的 AppDelegate.m/h 文件是很重要的呢,因为它是对 Application 的整个生命周期进行管理的. 先明白,每个iPhone应用程序都有一个UIApplication,UI ...

  8. ios开发之AppDelegate

    创建应用程序之后之后,默认有AppDelegate.h文件与AppDelegate.m文件.   AppDelegate为何物?  AppDelegate为整个应用的一个代理,提供程序启动.退出等类似 ...

  9. 【转】ios开发之AppDelegate

    创建应用程序之后之后,默认有AppDelegate.h文件与AppDelegate.m文件.   AppDelegate为何物?  AppDelegate为整个应用的一个代理,提供程序启动.退出等类似 ...

随机推荐

  1. js:语言精髓笔记13--语言技巧

    消除代码全局变量名占用: //本质是使用匿名函数: void function(x, y, z) { console.log(x + y + z); }(1,2,3); //要使函数内的变量不被释放, ...

  2. iOS数组排序

    [_fields sortUsingComparator:^NSComparisonResult(UITextField *obj1, UITextField *obj2) { /* NSOrdere ...

  3. ural 1433. Diamonds

    1433. Diamonds Time limit: 1.0 secondMemory limit: 64 MB Sasha is lucky to have a diamond in the for ...

  4. 常用元素默认margin和padding值问题探讨

    关于默认元素在不同浏览器中的margin值是多少的问题,今天做了一个探讨 复制代码 代码如下: // body的margin值 firefox 20.0 ----------------------- ...

  5. DataTable.Compute方法使用实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. POJ 3411 Paid Roads(DFS)

    题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...

  7. 实例讲解Linux下的makefile

    1.程序代码结构如下 makefile/ |-- Makefile |-- haha.c `-- hehe.c 1.1.需要被编译的源代码如下 $ cat haha.c #include " ...

  8. Maya 学习资料

    罗其胜3d角色强化 CGwhat-Maya变形金刚擎天柱建模教程 Pixar in the box - khan academy Siggraph历届优秀动画 CG软件发展史:MAYA动画十年历程 m ...

  9. Win2K3一句话开3389

    网上流传了很多开3389的exe小工具,还有读写注册表的bat批处理,有的是操作注册表,有的是操作各种组件这里提供一个不会被拦截,而且可以使用的开3389的cmd一句话: wmic RDTOGGLE  ...

  10. /etc/named/named.conf.options中的Options参数

    listen-on port 53 { any; }; 监听在这部主机系统上面的哪个网路介面.预设是监听在localhost,亦即只有本机可以对DNS 服务进行查询,那当然是很不合理啊!所以这里要将大 ...