欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

iOS学习之应用程序的启动原理,ios应用程序原理

来源: 开发者 投稿于  被查看 27120 次 评论:117

iOS学习之应用程序的启动原理,ios应用程序原理


最近看视频了解了一下应用程序的启动原理,这里就做一个博客和大家分享一下,相互讨论,如果有什么补充或不同的意见可以提出来!

1、程序入口

 众所周知,一个应用程序的入口一般是一个 main 函数,iOS也不例外,在工程的 Supporting Files 文件夹中你可以找到main.m,他就是程序的入口。

  代码:

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

2、UIApplicationMain函数参数解析

 UIApplicationMain 函数的声明:

// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no
// NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init.
int UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName);
  • 第一个参数 argc 和 第二个参数 argv 为C语言的值,在这里可以不做考虑。

  • 第三个参数 principalClassName:主程序类名,由英文注释可知,当值为 nil 时,默认使用的就是 UIApplication

  • 第四个参数:delegateClassName:代理类名,在 iOS 中默认就是随工程一起创建出来的AppDelegate。

  第四个参数的类型是一个字符串,但是在主函数却是 NSStringFromClass([AppDelegate class])NSStringFromClass() 函数是将一个类名转为字符串的一种特殊转换函数,[AppDelegate class] 则是反射得到AppDelegate 对象的类名。

  和 NSStringFromClass() 函数类似的一系列函数声明:

FOUNDATION_EXPORTNSString *NSStringFromSelector(SEL aSelector); // 函数名转字符串
FOUNDATION_EXPORT SEL NSSelectorFromString(NSString *aSelectorName); // 字符串转函数名

FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass); // 类名转字符串
FOUNDATION_EXPORT Class __nullable NSClassFromString(NSString *aClassName); // 字符串转类名

FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) NS_AVAILABLE(10_5, 2_0); // 协议名转字符串
FOUNDATION_EXPORT Protocol * __nullable NSProtocolFromString(NSString *namestr) NS_AVAILABLE(10_5, 2_0); // 字符串转协议名

3、UIApplicationMain底层实现

(1)根据 principalClassName 提供类名创建 UIApplication 对象

(2)创建 UIApplicationDelegate 对象,并且成为 UIApplication 对象代理,app.delete = delegate

(3)开启一个主运行循环,处理事件,可以保持程序一直运行。

(4)加载 info.plist,并且判断 Main Interface 有木有指定 main.storyboard,如果指定,就会去加载

(5)如果有指定,加载 main.stroyboard 做的事情

  创建窗口,也就会说执行 AppDelegate 中的代理方法

  加载 main.storyboard, 并且加载 main.storyboard 指定的控制器

  把新创建的控制器作为窗口的跟控制器,让窗口显示出来

(6)如果没有指定,就在 AppDelegate 的代理方法 - (BOOL)application: didFinishLaunchingWithOptions:创建一个UIWindow 对象作为主窗口

  代码:

// 程序启动完成的时候
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 1.创建窗口,注意窗口必须要有尺寸,尺寸跟屏幕一样大的尺寸,窗口不要被释放
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];
    
    // 2.创建窗口的根控制器
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];
    
    [vc.view addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];
    
    // 如果设置窗口的跟控制器,默认就会把控制器的view添加到窗口上
    // 设置窗口的跟控制器,默认就有旋转功能
    self.window.rootViewController = vc;
    
    // 3.显示窗口
    [self.window makeKeyAndVisible];
    
    return YES;
}

 图解:  官方帮助文档图解:

 

用户评论