iOS程序启动程序原理

iOS(程序启动程序原理)

新建一个工程选择Single View Application(单视图控制器)

程序开始main.m

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

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

对于main.m的解读,由于objc是由c发展而来的面相对象语言,所以程序开始是由main函数进入,我们看到@autoreleasepool 将一段代码框住,autoreleasepool是什么呢,autoreleasepool自动引用计数池,也就是我们说的ARC,有了这个自动引用计数池,苹果将自动帮我们管理应用程序内存,之后 UIApplicationMain这个函数,UIApplicationMain这个函数会自动创建一个UIApplication对象,并创建UIApplicationDelegate对象,将UIApplication的delegate设置给UIApplicationDelegate对象,所以在项目一创建会有AppDelegate这个类。

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
//程序启动完毕之后会自动调用这个方法,可以在这个方法中设置我们的程序入口控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    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 invalidate graphics rendering callbacks. 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 active 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:.
}

并且在设置完成代理对象之后会开启一个主运行循环,也就是我们的 [NSRunLoop mainRunLoop],在主运行循环中添加任务,(如Timer任务,Source1,Source2(port任务))这样主线程就会一直不死,等待任务并执行下去,当没有任务时,主运行循环会休眠,并在这个时候释放自动释放池,等待再次的唤醒。(设置主运行循环之后)并查看info.plist程序配置文件(一个程序也只能有一个info.plist文件)查看是否设置程序启动入口为main.storyboard。

设置main.stroyboard

找到main.storyboard中的箭头所指向的控制器,查看一下控制器类型ViewController所以程序加载完毕之后会创业ViewController这个类。Assets.xcassets 这个文件夹用于放images文件。这就是对于程序启动原理的解答。我们写代码可以在ViewController中进行,也可自己创建一个新类进行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值