老生常谈iOS应用程序生命周期

 更新时间:2017年04月23日 09:30:13   投稿:jingxian  
下面小编就为大家带来一篇老生常谈iOS应用程序生命周期。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

开发应用程序都要了解其生命周期。

今天我们接触一下iOS应用程序的生命周期, iOS的入口在main.m文件:

int main(int argc, char * argv[]) { 
  @autoreleasepool { 
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 
  } 
} 

main函数的两个参数,iOS中没有用到,包括这两个参数是为了与标准ANSI C保持一致。UIApplicationMain函数,前两个和main函数一样,重点是后两个,官方说明是这样的:

// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no 
// NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init. 
UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName); 

如果主要类(principal class)为nil,将从Info.plist中获取,如果Info.plist中不存在对应的key,则默认为UIApplication;如果代理类(delegate class)将在新建工程时创建。

根据UIApplicationMain函数,程序将进入AppDelegate.m,这个文件是xcode新建工程时自动生成的。

应用程序的生命周期(AppDelegate.m):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
  // Override point for customization after application launch. 
  NSLog(@"iOS_didFinishLaunchingWithOptions"); 
  return YES; 
} 
 
- (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. 
  NSLog(@"iOS_applicationWillResignActive"); 
} 
 
- (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. 
  NSLog(@"iOS_applicationDidEnterBackground"); 
} 
 
- (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. 
  NSLog(@"iOS_applicationWillEnterForeground"); 
} 
 
- (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. 
  NSLog(@"iOS_applicationDidBecomeActive"); 
} 
 
- (void)applicationWillTerminate:(UIApplication *)application { 
  // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
  NSLog(@"iOS_applicationWillTerminate"); 
}

1、application didFinishLaunchingWithOptions:当应用程序启动时执行,应用程序启动入口,只在应用程序启动时执行一次。若用户直接启动,lauchOptions内无数据,若通过其他方式启动,lauchOptions包含对应方式的内容。

2、applicationWillResignActive:在应用程序将要由活动状态切换到非活动状态时候,要执行的委托调用,如 按下 home 按钮,返回主屏幕,或全屏之间切换应用程序等。

3、applicationDidEnterBackground:在应用程序已进入后台程序时,要执行的委托调用。

4、applicationWillEnterForeground:在应用程序将要进入前台时(被激活),要执行的委托调用,刚好与applicationWillResignActive 方法相对应。

5、applicationDidBecomeActive:在应用程序已被激活后,要执行的委托调用,刚好与applicationDidEnterBackground 方法相对应。

6、applicationWillTerminate:在应用程序要完全推出的时候,要执行的委托调用,这个需要要设置UIApplicationExitsOnSuspend的键值。

初次启动:

2013-05-24 20:20:31.550 LifeIOS[451:c07] iOS_didFinishLaunchingWithOptions

2013-05-24 20:20:31.551 LifeIOS[451:c07] iOS_applicationDidBecomeActive

按下home键:

2013-05-24 20:22:17.349 LifeIOS[451:c07] iOS_applicationWillResignActive

2013-05-24 20:22:17.350 LifeIOS[451:c07] iOS_applicationDidEnterBackground

点击程序图标进入:

2013-05-24 20:22:56.913 LifeIOS[451:c07] iOS_applicationWillEnterForeground

2013-05-24 20:22:56.914 LifeIOS[451:c07] iOS_applicationDidBecomeActive

程序中没有设置UIApplicationExitsOnSuspend的值,程序不会彻底退出。

以上这篇老生常谈iOS应用程序生命周期就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • iOS使用核心动画和粒子发射器实现点赞按钮的方法

    iOS使用核心动画和粒子发射器实现点赞按钮的方法

    这篇文章主要给大家介绍了iOS如何使用核心动画和粒子发射器实现点赞按钮的方法,文中给出了详细的示例代码,相信对大家的理解和学习具有一定的参考借鉴,有需要的朋友们下面跟着小编一起来学习学习吧。
    2016-12-12
  • iOS实现抽屉效果

    iOS实现抽屉效果

    这篇文章主要为大家详细介绍了iOS实现抽屉效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • iOS实现第三方微信登录方式实例解析(最新最全)

    iOS实现第三方微信登录方式实例解析(最新最全)

    这篇文章主要介绍了iOS实现第三方微信登录方式实例解析(最新最全),非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-12-12
  • iOS开发tips-UINavigationBar的切换效果

    iOS开发tips-UINavigationBar的切换效果

    这篇文章主要为大家详细介绍了iOS开发tips-UINavigationBar的切换效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • iOS实现计算器小功能

    iOS实现计算器小功能

    这篇文章主要介绍了iOS实现计算器小功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键

    iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键

    这篇文章主要介绍了iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键 的相关资料,需要的朋友可以参考下
    2016-08-08
  • iOS CAReplicatorLayer实现脉冲动画效果

    iOS CAReplicatorLayer实现脉冲动画效果

    这篇文章主要介绍了iOS CAReplicatorLayer实现脉冲动画效果 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • 源码解析ios开发SDWebImage方法

    源码解析ios开发SDWebImage方法

    这篇文章主要为大家介绍了源码解析ios开发SDWebImage方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • IOS实现视频动画效果的启动图

    IOS实现视频动画效果的启动图

    这篇文章实现的是一个关于启动页或者引导页的视频动画效果的实现过程,对于大家开发APP具有一定的参考借鉴价值,有需要的可以来看看。
    2016-09-09
  • iOS仿微信摇一摇功能

    iOS仿微信摇一摇功能

    这篇文章主要为大家详细介绍了iOS仿微信摇一摇功能的实现方法 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05

最新评论