zoukankan      html  css  js  c++  java
  • 005-iOS App程序启动过程

      掌握

      •  1.项目中常见文件(Info.plist和pch文件的作用)
     
      •  2.UIApplication
     
      •  3.AppDelegate的代理方法
     
      •  4.iOS程序的完整启动过程(UIApplication、AppDelegate、UIWindow、UIViewController的关系)
     
    • 1.项目中常见文件(Info.plist和pch文件的作用)
    • Info.plist

      1>Info.plist常见的设置

    •建立一个工程后,会在Supporting files文件夹下看到一个“工程名-Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除
      
    •在旧版本Xcode创建的工程中,这个配置文件的名字就叫“Info.plist”
    •项目中其他Plist文件不能带有“Info”这个字眼,不然会被错认为是传说中非常重要的“Info.plist”
    •项目中还有一个InfoPlist.strings的文件,跟Info.plist文件的本地化相关

      2>常见属性(红色部分是用文本编辑器打开时看到的key)

      •  Localiztion native development region(CFBundleDevelopmentRegion)-本地化相关
     
      •  Bundle display name(CFBundleDisplayName)-程序安装后显示的名称,限制在10-12个字符,如果超出,将被显示缩写名称
     
      •  Icon file(CFBundleIconFile)-app图标名称,一般为Icon.png
     
      •  Bundle version(CFBundleVersion)-应用程序的版本号,每次往App Store上发布一个新版本时,需要增加这个版本号
     
      •  Main storyboard file base name(NSMainStoryboardFile)-主storyboard文件的名称
     
      •  Bundle identifier(CFBundleIdentifier)-项目的唯一标识,部署到真机时用到
    • pch文件

      •  项目的Supporting files文件夹下面有个“工程名-Prefix.pch”文件,也是一个头文件
     
      •  pch头文件的内容能被项目中的其他所有源文件共享和访问
     
      •  一般在pch文件中定义一些全局的宏
     
      •  在pch文件中添加下列预处理指令,然后在项目中使用Log(…)来输出日志信息,就可以在发布应用的时候,一次性将NSLog语句移除(在调试模式下,才有定义DEBUG)

        

    #ifdef DEBUG
    #define Log(...) NSLog(__VA_ARGS__)
    #else
    #define Log(...) /* */
    #endif

    • 2.UIApplication

    • 什么是UIApplication

      •  UIApplication对象是应用程序的象征
     
      •  每一个应用都有自己的UIApplication对象,而且是单例的
     
      •  通过[UIApplication sharedApplication]可以获得这个单例对象
     
      •  一个iOS程序启动后创建的第一个对象就是UIApplication对象
     
      •  利用UIApplication对象,能进行一些应用级别的操作
    • UIApplication的常用属性

      •  设置应用程序图标右上角的红色提醒数字

      @property(nonatomic) NSInteger applicationIconBadgeNumber;  如写入下列代码后,应用图标将显示:

    [UIApplication sharedApplication].applicationIconBadgeNumber = 10;  
      •  设置联网指示器的可见性

     @property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible; 如写入下列代码后,应用图标将显示:

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    • UIApplication之强大的openURL方法

    UIApplication有个功能十分强大的openURL:方法
    - (BOOL)openURL:(NSURL*)url;
    
    openURL:方法的部分功能有
    // 打电话
    UIApplication *app = [UIApplication sharedApplication];
    [app openURL:[NSURL URLWithString:@"tel://10086"]];
    
    // 发短信
    [app openURL:[NSURL URLWithString:@"sms://10086"]];
    
    // 发邮件
    [app openURL:[NSURL URLWithString:@"mailto://841657484@qq.com"]];
    
    // 打开一个网页资源
    [app openURL:[NSURL URLWithString:@"http://home.cnblogs.com/u/lszwhb/"]];
    
    通过上面的方法可以打开对应的其他app程序 不用你考虑要打开什么应用程序,系统会自动识别帮你打开
    • iOS7中的状态栏

      •  从iOS7开始,系统提供了2种管理状态栏的方式
         1. 通过UIViewController管理(每一个UIViewController都可以拥有自己不同的状态栏)
         2. 通过UIApplication管理(一个应用程序的状态栏都由它统一管理)

        ps: ios7之前,状态栏由UIApplication管理,状态栏和UIViewController是分离独立的。

      •  在iOS7中,默认情况下,状态栏都是由UIViewController管理的,UIViewController实现下列方法就可以轻松管理状态栏的可见性和样式
          • 状态栏的样式

          - (UIStatusBarStyle)preferredStatusBarStyle;

          • 状态栏的可见性

          - (BOOL)prefersStatusBarHidden;  例如在UIViewController中写入下列方法,状态栏就隐藏了

    - (BOOL)prefersStatusBarHidden
    {
        return YES;
    }
          •利用UIApplication来管理状态栏
        如果想利用UIApplication来管理状态栏,首先得修改Info.plist的设置  
     

    • 3.UIApplicationDelegate(AppDelegate的代理方法)

    •所有的移动操作系统都有个致命的缺点:app很容易受到打扰。比如一个来电或者锁屏会导致app进入后台甚至被终止
    •还有很多其它类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate代理来处理这些系统事件
    •delegate可处理的事件包括:
    Ø应用程序的生命周期事件(如程序启动和关闭)
    Ø系统事件(如来电)
    Ø内存警告
    Ø… …
               
    Ø每次新建完项目,都有个带有“AppDelegate”字眼的类,它就是UIApplication的代理    
    ØAPAppDelegate默认已经遵守了UIApplicationDelegate协议,已经是UIApplication的代理
                      
     
    ØAppDelegate的代理方法如下:
    /**
     *  app启动完毕后就会调用
     */
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        return YES;
    }
    /**
     *  app程序失去焦点就会调用
     */                        
    - (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.
    }
    
    /**
     *  app进入后台的时候调用
     *
     *  一般在这里保存应用的数据(游戏数据,比如暂停游戏)
     */
    - (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.
    }
    /**
     *  app程序程序从后台回到前台就会调用
     */
    - (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.
    }
    /**
     *  app程序获取焦点就会调用
     */
    - (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)applicationDidReceiveMemoryWarning:(UIApplication *)application
    {
    }
    /**
     *  程序即将退出调用
     */
    - (void)applicationWillTerminate:(UIApplication *)application
    {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    AppDelegate代理方法

     • 4.iOS程序的完整启动过程(UIApplication、AppDelegate、UIWindow、UIViewController的关系)

    程序启动的完整过程

    1.main函数

    2.UIApplicationMain

    * 创建UIApplication对象

    * 创建UIApplication的delegate对象

    3.delegate对象开始处理(监听)系统事件(没有storyboard)

    * 程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法

    * 在application:didFinishLaunchingWithOptions:中创建UIWindow

    * 创建和设置UIWindow的rootViewController

    * 显示窗口

    3.根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard(有storyboard)

    * 创建UIWindow

    * 创建和设置UIWindow的rootViewController

    * 显示窗口

     
     
  • 相关阅读:
    算法总结7—多维缩放
    算法总结3—神经网络
    算法总结9—优化
    算法总结8—非负矩阵因式分解
    R语言系列—区间估计
    算法总结2—决策树分类器
    算法总结5&6k最近邻与聚类
    统计,逻辑与智能
    算法总结4—支持向量机
    R语言系列—回归分析
  • 原文地址:https://www.cnblogs.com/lszwhb/p/3905749.html
Copyright © 2011-2022 走看看