The Swift Code之应用程序的启动过程

The Swift Code主要是通过编写代码来完成应用程序的实现,使我们更能够深入的了解其编程语言实现的原理.也能够辅助更快的使用XCODE开发应用程序.

这篇文章主要是讲解启动应用程序从代码入口.

其 实这里主要是通过注解来实现的,新建IOS swift项目的时候,会生成一个AppDelegate文件,这个文件就是应用程序的代码入口,在声明类的同时加入了注解 @UIApplicationMain,表明这个应用程序.其实在这之前,我们必须在配置文件里设置启动入口为Main

4801DAAE-B3CF-4446-B37D-97761FC4D9BC.png

以下讲解,我们在代码里做讲解,大家可以试试在模拟里调试

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    /*程序未启动时,点击应用程序触发该方法,之后触发applicationDidBecomeActive*/
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        NSLog("开始启动application........")
        
        window =  UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.orangeColor()
        
        
        window?.rootViewController = ViewController()
        
                 
        return true
    }
    
    /*点击HOme键后,程序即将进入委托处理(委托给系统处理),紧跟着将会触发 applicationDidEnterBackground 方法*/
    func applicationWillResignActive(application: UIApplication) {
        // 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.
        NSLog("开始启动applicationWillResignActive........")
    }
    /*用户点击了Home按键,程序进入后台处理*/
    func applicationDidEnterBackground(application: UIApplication) {
        // 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.
        NSLog("开始启动applicationDidEnterBackground........")
    }
    
    /*在应用程序未消亡状态,状态由后台处理,进入前台处理,触发该方法,之后触发applicationDidBecomeActive*/
    func applicationWillEnterForeground(application: UIApplication) {
        // 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.
        NSLog("开始启动applicationWillEnterForeground........")
    }
    /*应用程序进入后,触发该方法*/
    func applicationDidBecomeActive(application: UIApplication) {
        // 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.
        NSLog("开始启动applicationDidBecomeActive........")
    }
    
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        NSLog("开始启动applicationWillTerminate........")
    }
    
    
}

本文属于吴统威的博客原创文章,转载时请注明出处及相应链接:http://www.wutongwei.com/front/infor_showone.tweb?id=87

猜你喜欢

转载自towaywu.iteye.com/blog/2199854