iOS 底层原理

iOS的生命周期

2018-01-16  本文已影响11人  青青草原饲养源与园叮叮叮叮

APP的生命周期

像Android apps 一样有生命周期。Apps就是用户自定义的代码,需要利用系统提供的资源完成特定的任务,所以了解Apps的生命周期是必要的。

APP 的main()函数

iOS系统是基于c 语言的,其Apps的入口当然也是main函数,不过main函数不用自己写,有创建工程时自动完成。如图下代码:

#import "AppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

这个main函数的主要作用是将控制权交给UIKit framework。[UIApplicationMain] 函数负责创建App 核心对象,通storyboard文件中加载用户界面,调用自定义代码做初始化工作。

APP的结构

在启动的期间,UIApplicationMain 函数创建几个关键对象开始运行APP,其中最关键是 UIApplication对象,这个对象让APP和系统交互更加便利。 如图2,这是大多数APP含有的model-view-controller架构模式,这个模式将用户数据、业务逻辑从界面中分离,并有利于iOS设备的适配。`

APP的状态

APP具有以下几种状态,我们主要使用到的是Active和Background状态。在Active状态时,用户程序处于前台与用户交互处理相关事件。进入后台时可以进行音乐播放、录音、位置更新、等操作。下图为APP的状态图:


image.png
//启动程序是最先调用的函数
-(BOOL) application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    return YES;
}
//在app显示给用户之前,最后的初始化操作。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

/*通知APP将要变成前台程序,这里可以重启被暂停的任务,如果此程序在后台进入可以选择更新用户界面。
 */
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

/**
 * 通知 APP 将进入前台,
 *这里可以撤销一些在为后台运行而做的改变。
 **/

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


/**通知APP将要离开前台,(依次进行inActive -》background-》-Suspended)。如果有电话接入、SMS消息、是用户退出、或者想后台切换都会调用此函数。
 *应该使用这个方法暂停正在执行的任务,取消定时器、取消图形渲染回调,暂停游戏等操作。
 */
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
/**
 *通知APP将要进入后台状态,随时可能被挂起。
 *应该使用这个方法:释放分享的资源,存储用户数据、取消定时器、保存APP的状态消息以用于恢复。
 *如果APP支持后台运行,当用户退回时,调用此方法而不调用applicationWillTerminate。
 **/

- (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.
}

/**
*通知 APP 将要终止,如果程序被挂起将不调用。
*保存数据
**/
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
上一篇下一篇

猜你喜欢

热点阅读