程序启动和运行过程

            摘抄自:http://www.cnblogs.com/kenshincui/p/3890880.html#runProcess

程序的运行过程

在几乎所有的程序开发中程序一般都是从main函数开始运行的,那么IOS程序也不例外,在上图中我们可以看到Xcode为我们生成了一个main.m文件:

//
//  main.m
//  FirstIOS
//
//  Created by Kenshin Cui on 14-2-23.
//  Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

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

这个默认的iOS程序就是从main函数开始执行的,但是在main函数中我们其实只能看到一个方法,这个方法内部是一个消息循环(相当于一个死循 环),因此运行到这个方法UIApplicationMain之后程序不会自动退出,而只有当用户手动关闭程序这个循环才结束。这个方法有四个参数:

  • 第一个参数和第二个参数其实就是main函数的参数,分别代表:参数个数、参数内容;
  • 第三个参数代表UIApplication类(或子类)字符串,这个参数默认为nil则代表默认为UIApplication类,用户 可以自定义一个类继承于这个类;如果为nil则等价于NSStringFromClass([UIApplication class]),大家可以自己试验,效果完全一样;UIApplication是单例模式,一个应用程序只有一个UIApplication对象或子对 象;
  • 第四个参数是UIApplication的代理类字符串,默认生成的是AppDelegate类,这个类主要用于监听整个应用程序生命 周期的各个事件(其实类似于之前我们文章中提到的事件监听代理),当UIApplication运行过程中引发了某个事件之后会调用代理中对应的方法;

小技巧:

其实在Xcode中如果要看一些系统方法的解释或者参数说明,可以直接鼠标放到这个方法上,在Xcode右侧面板中就会给出帮助提示,如下图当我们放到UIApplicationMain上之后:

也就是说当执行UIApplicationMain方法后这个方法会根据第三个参数创建对应的UIApplication对象,这个对象会根据第四 个参数AppDelegate创建并指定此对象为UIApplication的代理;同时UIApplication会开启一个消息循环不断监听应用程序 的各个活动,当应用程序生命周期发生改变UIApplication就会调用代理对应的方法。

既然应用程序UIApplication是通过代理和外部交互的,那么我们就有必要清楚AppDelegate的操作细节,下面是UIApplication详细的代码:

AppDelegate.h

//
//  AppDelegate.h
//
//
//  Created by Kenshin Cui on 14-2-23.
//  Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

//
//  AppDelegate.m
//
//
//  Created by Kenshin Cui on 14-2-23.
//  Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

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

- (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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

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

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

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

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

这个类中定义了应用程序生命周期中各个事件的执行方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;程序启动之后执行,只有在第一次程序启动后才执行,以后不再执行;

- (void)applicationWillResignActive:(UIApplication *)application;程序将要注销激活时(失去焦点)执行,程序失去焦点用户就不能再对程序进行操作;

- (void)applicationDidEnterBackground:(UIApplication *)application;程序进入后台后执行,注意进入后台时会先失去焦点再进入后台;

- (void)applicationWillEnterForeground:(UIApplication *)application;程序将要进入前台时执行;

- (void)applicationDidBecomeActive:(UIApplication *)application;程序被激活(获得焦点)后执行,注意程序被激活时会先进入前台再被激活;

- (void)applicationWillTerminate:(UIApplication *)application;程序在终止时执行,包括正常终止或异常终止,例如说一个应用程序在后太运行(例如音乐播放软件、社交软件等)占用太多内存这时会意外终止调用此方法;

为了演示程序的生命周期,不妨在每个事件中都输出一段内容,简单调整上面的代码:

AppDelegate.m

//
//  AppDelegate.m
//  FirstIOS
//
//  Created by Kenshin Cui on 14-2-23.
//  Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"程序已经启动...");
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"程序将要失去焦点...");
    // 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"程序已经进入后台...");
    // 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.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"程序将要进入前台...");
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"程序已经获得焦点...");
    // 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.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"程序将要终止...");
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

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

}

@end

下面是各个不同操作的运行结果:

相信通过上面运行过程大家会对整个运行周期有个大概了解。比较容易混淆的地方就是应用程序进入前台、获得焦点、失去焦点、进入后台,这几个方法大家要清楚。如果一个应用程序失去焦点那么意味着用户当前无法进行交互操作,因此一般会先失去焦点再进入后台防止进入后台过程中用户误操作;如果一个应用程序进入前台也是类似的,会先进入前台再获得焦点,这样进入前台过程中未完全准备好的情况下用户无法操作。另外一般如果应用程序要保存用户数据会在注销激活中进行 (而不是在进入后台方法中进行),因为如果用户双击Home不会进入后台只会注销激活;如果用户恢复应用状态一般在进入激活状态时处理(而不是在进入前台 方法中进行),因为用户可能是从任务栏直接返回应用,此时不会执行进入前台操作。

当然,上面的事件并不是所有AppDelegate事件,而是最常用的一些事件,其他事件大家可以查阅官方文档,例如 -(void)applicationDidReceiveMemoryWarning:(UIApplication *)application;用于在内存占用过多发出内存警告时调用并通知对应的ViewController调用其内存回收方法。这里简单以图形方式描 述一下应用程序的调用过程:

注意:从后台进入前台,不会调用控制器的viewDidLoad方法,也不会调用控制器的viewWillAppear方法。

时间: 2024-07-31 05:25:22

程序启动和运行过程的相关文章

程序启动的完整过程

程序启动的完整过程 int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([MJAppDelegate class])); } } 执行顺序 1.main函数 2.UIApplicationMain 创建UIApplication对象 创建UIApplication的delegate对象 3.开启主运行循环 3(1).de

如何在ASP.NET Core程序启动时运行异步任务(2)

原文:Running async tasks on app startup in ASP.NET Core (Part 2) 作者:Andrew Lock 译者:Lamond Lu 在我的上一篇博客中,我介绍了如何在ASP.NET Core应用程序启动时运行一些一次性异步任务.本篇博客将继续讨论上一篇的内容,如果你还没有读过,我建议你先读一下前一篇. 在本篇博客中,我将展示上一篇博文中提出的"在Program.cs中手动运行异步任务"的实现方法.该实现会使用一些简单的接口和类来封装应用

在 ASP.NET Core 程序启动前运行你的代码

原文:在 ASP.NET Core 程序启动前运行你的代码 一.前言# 在进行 Web 项目开发的过程中,可能会存在一些需要经常访问的静态数据,针对这种在程序运行过程中可能几乎不会发生变化的数据,我们可以尝试在程序运行前写入到缓存中,这样在系统后续使用时就可以直接从缓存中进行获取,从而减缓因为频繁读取这些静态数据造成的应用数据库服务器的巨大承载压力. 既然需要在程序运行前将静态数据写入到缓存中,毫无疑问我们需要在程序运行前执行一些自定义功能的代码,那么在本章中,我将会介绍如何在 ASP.NET

五、程序启动的完整过程

程序启动的完整过程: 1.先执行main函数,main内部会调用UIApplicationMain函数,该函数的声明如下: int UIApplicationMain(int argc, char argv[], NSString principalClassName, NSString *delegateClassName). argc.argv:标准main函数的参数,直接传递给UIApplicationMain进行相关处理即可 principalClassName:指定应用程序类,该类必须

41-50(UIApplication和delegate,UIApplicationMain,UIWindow,程序启动的完整过程,控制器view的延迟加载)

41.UIApplication和delegate 42.UIPickerView 43.UIDatePicker 44.程序启动的完整过程 45.UIApplicationMain 46.UIWindow 47.如何创建一个控制器 48.控制器view的延迟加载 49.多控制器 50.UINavigationController的使用步骤 { 这几天一直在赶项目, 今天终于闲下来了! 今天是个好日子,空间里满天的2014520 那么来看看我们程序员的爱情吧! 爱情就是死循环,一旦执行就陷进去了

UIPickerView/UIDatePicker/程序启动的完整过程

一.UIPickerView 1.UIPickerView的常见属性 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id<UIPickerViewDataSource> dataSource; 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择) @property(nonatomic,assign) id<UIPickerViewDelegate>   deleg

UIPickerView,程序启动的完整过程

一.UIPickerView1.UIPickerView的常见属性// 数据源(用来告诉UIPickerView有多少列多少行)@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;// 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)@property(nonatomic,assign) id<UIPickerViewDelegate>   dele

Objective - C 学习笔记:程序启动的完整过程

1. main函数 2. UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象 3.1 delegate代理开始处理(监听)系统事件  (没有storyboard) * 程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法 * 在application:didFinishLaunchingWithOptions:中创建UIWindow * 创建和

iOS程序启动的完整过程

1.main函数 2.UIApplicationMain* 创建UIApplication对象* 创建UIApplication的delegate对象 3.delegate对象开始处理(监听)系统事件(没有storyboard)* 程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法* 在application:didFinishLaunchingWithOptions:中创建UIWindow* 创建和设置UIWindow的