觉得应该从头梳理一下
ios 作为一个移动操作系统,担负着管理手机资源的任务,不管是文件系统,还是进程调度,还是内存调度,都是因为有了 ios 对底层硬件的管理,才有了上层应用程序的顺利执行.....
作为一款操作系统,肯定和pc平台的操作系统在原理上没有什么异同,都是加电复位寄存器(ip)地址,指向将要进行加载的第一行程序,在 cpu 的顺序执行下加载操作系统,然后常驻系统监听,对打开的应用程序进行管理
我们按下应用程序的那一刻 : 到使用起来,这之间又发生了什么呢?
众所周知,应用程序只有在内存中执行才算是真正的进程......
当我们按下应用程序图标后.操作系统检测到这个事件,然后通过事件传递处理到这个图标,内存开始加载资源, 保存数据,IP指针指到将要运行的代码快处,进入用户态模式,开始执行代码..也就是我们写的源文件编译过后的二进制.......
这里就要看官方文档了
The Main Function
The entry point for every C-based app is the main function and iOS apps are no different. What is different is that for iOS apps you do not write the main function yourself. Instead, Xcode creates this function as part of your basic project. Listing 2-1 shows an example of this function. With few exceptions, you should never change the implementation of the main function that Xcode provides.
翻译:正如每个基于c的app一样,ios的app进入点都是一个 main 函数,不同之处在于你不需要手动书写这个主函数,xcode 会自动创建,也最好不要随便改动 代码如下:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
The main function of an iOS app
The only thing to mention about the main function is that its job is to hand control off to the UIKit framework. The UIApplicationMain function handles this process by creating the core objects of your app, loading your app’s user interface from the available storyboard files, calling your custom code so that you have a chance to do some initial setup, and putting the app’s run loop in motion. The only pieces that you have to provide are the storyboard files and the custom initialization code.
上面解释了主函数的作用
- 创造核心对象
- 控制 uikit 层
- 加载 storyboard 加载初始化代码
- 加载 runloop 监听
上面这个图是我们最熟系的 mvc设计模式的官方图片...可以清楚的看到 uiapplication 和 uiwindow 是系统的.所以一般不要去继承这两个类...
完成之后就开启了main runloop 循环 对于 runloop 的介绍,还是看官方比较权威
The Main Run Loop
An app’s main run loop processes all user-related events. The UIApplication object sets up the main run loop at launch time and uses it to process events and handle updates to view-based interfaces. As the name suggests, the main run loop executes on the app’s main thread. This behavior ensures that user-related events are processed serially in the order in which they were received.
As the user interacts with a device, events related to those interactions are generated by the system and delivered to the app via a special port set up by UIKit. Events are queued internally by the app and dispatched one-by-one to the main run loop for execution. The UIApplication object is the first object to receive the event and make the decision about what needs to be done. A touch event is usually dispatched to the main window object, which in turn dispatches it to the view in which the touch occurred. Other events might take slightly different paths through various app objects.
Many types of events can be delivered in an iOS app.. Many of these event types are delivered using the main run loop of your app, but some are not. Some events are sent to a delegate object or are passed to a block that you provide. For information about how to handle most types of events—including touch, remote control, motion, accelerometer, and gyroscopic events
从上图中我们可以看到 main runloop 的作用就是传递许多中类型事件(但不是全部),application 是第一个接收到的对象,,然后根据事件进行派遣,一些会调用自定义的 delegate 代理(protocal 实现),一些会调用block....但都是因为有事件派遣产生...
应用程序状态转换
Most state transitions are accompanied by a corresponding call to the methods of your app delegate object. These methods are your chance to respond to state changes in an appropriate way. These methods are listed below, along with a summary of how you might use them.
application:willFinishLaunchingWithOptions:—This method is your app’s first chance to execute code at launch time.
application:didFinishLaunchingWithOptions:—This method allows you to perform any final initialization before your app is displayed to the user.
applicationDidBecomeActive:—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation.
applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.
applicationDidEnterBackground:—Lets you know that your app is now running in the background and may be suspended at any time.
applicationWillEnterForeground:—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active.
applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended.
在产生了main runloop 之后,发生了什么呢?
现在应用程序已经有了,但是还没有任何可以显示的东西...
- 如果有 storyboard 且 info.plist 配置正确的话,系统会自动创建uiwindow,作为 appdelegate的一个属性,自动加载 storyboard 中箭头指向的的控 制器,作为uiwindow 的根控制器,加载视图显示.......(自动做了没有的步骤而已)
- 如果没有的话, 就要自己在程序方法中设置
程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法
在application:didFinishLaunchingWithOptions:中创建UIWindow
创建和设置UIWindow的rootViewController
显示窗口
之后事件循环监听,就如图二一样,开始使用......(未完)