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

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

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

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

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

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

importUIKit

@UIApplicationMain
classAppDelegate:UIResponder,UIApplicationDelegate{

varwindow:UIWindow?

/*程序未启动时,点击应用程序触发该方法,之后触发applicationDidBecomeActive*/
funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:[NSObject:AnyObject]?)->Bool{
//Overridepointforcustomizationafterapplicationlaunch.

NSLog("开始启动application........")

window=UIWindow(frame:UIScreen.mainScreen().bounds)
window?.backgroundColor=UIColor.orangeColor()


window?.rootViewController=ViewController()


returntrue
}

/*点击HOme键后,程序即将进入委托处理(委托给系统处理),紧跟着将会触发applicationDidEnterBackground方法*/
funcapplicationWillResignActive(application:UIApplication){
//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.
//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.
NSLog("开始启动applicationWillResignActive........")
}
/*用户点击了Home按键,程序进入后台处理*/
funcapplicationDidEnterBackground(application:UIApplication){
//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.
//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.
NSLog("开始启动applicationDidEnterBackground........")
}

/*在应用程序未消亡状态,状态由后台处理,进入前台处理,触发该方法,之后触发applicationDidBecomeActive*/
funcapplicationWillEnterForeground(application:UIApplication){
//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.
NSLog("开始启动applicationWillEnterForeground........")
}
/*应用程序进入后,触发该方法*/
funcapplicationDidBecomeActive(application:UIApplication){
//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.
NSLog("开始启动applicationDidBecomeActive........")
}

funcapplicationWillTerminate(application:UIApplication){
//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.
NSLog("开始启动applicationWillTerminate........")
}


}


转载至吴统威的博客:http://www.wutongwei.com/front/infor_showone.tweb?id=87

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...