2016-03-15 11:37:26 LingYu_iOS 阅读数 2604


    // 当应用程序启动后将会调用该方法
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        // 创建UIWindow对象,并初始化该窗口的大小与主屏幕大小相同
        let rect : CGRect = UIScreen.mainScreen().bounds
        // 程序将创建的UIWindow对象赋值给该程序委托对象的window属性
        self.window = UIWindow(frame:rect)
        // 创建ViewController对象、并使用ViewController界面布局文件来
        // 初始化该视图控制器关联的用户界面
        let vc = ViewController(nibName: "ViewController", bundle: nil)
        // 让该程序的窗口加载、并显示viewController视图控制器关联的用户界面
        self.window?.rootViewController = vc
        self.window?.backgroundColor = UIColor.whiteColor()
        // 将该UIWindow对象设为主窗口、并显示出来
        return true


// 当应用程序启动后将会调用该方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    // 创建UIWindow对象,并初始化该窗口的大小与主屏幕大小相同
    CGRect rect = [[UIScreen mainScreen] bounds];
    // 程序将创建的UIWindow对象赋值给该程序委托对象的window属性
    self.window = [[UIWindow alloc] initWithFrame:rect];
    // Override point for customization after application launch.
    // 创建FKViewController对象、并使用FKViewController界面布局文件来
    // 初始化该视图控制器关联的用户界面
    ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    // 让该程序的窗口加载、并显示viewController视图控制器关联的用户界面
    self.window.rootViewController = vc;
    self.window.backgroundColor = [UIColor whiteColor];
    // 将该UIWindow对象设为主窗口、并显示出来
    [self.window makeKeyAndVisible];
    return YES;

2014-07-17 16:17:08 qinken547 阅读数 576
application:didFinishLaunchingWithOptions: is where you instantiate and set an
application’s root view controller.
This method gets called exactly once when the application has launched. Even if you go to
another app and come back, this method does not get called again. If you reboot your phone and
start the app again, application:didFinishLaunchingWithOptions: will get called again.
initWithNibName:bundle: is the designated initializer for UIViewController.
When a view controller instance is created, its initWithNibName:bundle: gets called once. Note
that in some apps, you may end up creating several instances of the same view controller class.
This method will get called once on each as it is created.
loadView: is overridden to create a view controller’s view programmatically.
• viewDidLoad can be overridden to configure views created by loading a NIB file. This method
gets called after the view of a view controller is created.
viewWillAppear: can be overridden to configure views created by loading a NIB file.
This method and viewDidAppear: will get called every time your view controller is moved on
screen. viewWillDisappear: and viewDidDisappear: will get called every time your view
controller is moved offscreen. So if you launch the app you are working on and hop back and
forth between Hypnosis and Reminder, BNRReminderViewController’s viewDidLoad method will
be called once, but viewWillAppear: will be called dozens of times.
2016-12-12 11:17:59 ZY_FlyWay 阅读数 1325

今天遇到个问题,想在didFinishLaunchingWithOptions方法里获取storyBoard的rootViewController。然后监听TabbarViewControllerde 点击事件(我的项目是storyBoard --> TabbarViewController形式)

问题一: didFinishLaunchingWithOptions 和 storyBoard 创建的方法,是怎么一个顺序呢。

  1. mian文件主函数创建UIApplication
  2. UIApplication 对象扫描 Info.plist 文件,将其中 Mainstoryboard file base name 所指定的 Storyboard 文件进行装载
  3. UIApplication 获取窗口并关联
  4. Storyboard 初始化 ,并指定root view controller
  5. 最后执行didFinishLaunchingWithOptions


跟踪一下找到了rootViewController ,获取如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.


    UITabBarController  *  tabBar  =  (UITabBarController *)application.delegate.window.rootViewController;

    tabBar.delegate  = self;


    return YES;


2017-05-05 11:14:19 Felicity294250051 阅读数 994

在项目中多模块的初始化工作大都写在的 AppDelegate.m 下的 - application: didFinishLaunchingWithOptions: 方法中去执行,这种做法使得该方法中代码过长、阅读性很差,这么做其实只是为了在程序启动时获得一次调用机会。我们可以采取下面的方法来减轻 AppDelegate.m 的压力:

+ (void)load {
    /** 注册监听一个通知 */
    __block id observer =
    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
         *  这里可以写你要初始化去做的东西
        /** 完成操作后销毁通知的监听 */
        [[NSNotificationCenter defaultCenter] removeObserver:observer];

额外说明一下 + load 和 + initialize 方法的区别:

+ load 方法会在加载类的时候就被调用,也就是 iOS 应用启动的时候,就会加载所有的类,就会调用每个类的 + load 方法。

+ initialize 方法会在第一次初始化这个类之前被调用。




2015-04-13 20:55:22 u014473564 阅读数 496


NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (dictionary)
        if (codition) {
            NSURL *url = [NSURL URLWithString:[dictionary objectForKey:@"url"]];
            [[UIApplication sharedApplication] openURL:url];


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


dispatch_async(dispatch_get_main_queue(), ^{
    [[UIApplication sharedApplication] openURL:url];