iOS 技术文章

AppDelegate中常用的代理方法回调的时机

2015-10-21  本文已影响4815人  爱掏蜂窝的熊

转载

介绍

本篇文章主要介绍一些UIApplicationDelegate中几个常用的代理方法 (回调方法)的调用时机。帮助你判断哪些方法到底放在哪个代理方法 (回调方法)中去实现。

方法如下:

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
// 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)applicationDidBecomeActive:(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.
// 当应用从活动状态主动变为非活动状态的时候
– (void)applicationWillResignActive:(UIApplication *)application;
– (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;

替代为####

这个方法在iOS9中被别的方法取代了,不建议使用了。

// 当用户通过其它应用启动本应用时,会回调这个方法,url参数是其它应用调用openURL:方法时传过来的。
– (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation NS_AVAILABLE_IOS(4_2);

建议使用下面这个方法,替换掉##

 - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options NS_AVAILABLE_IOS(9_0); // no equiv. notification. return NO if the application can't open for some reason
// try to clean up as much memory as possible. next step is to terminate app
– (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:
 – (void)applicationWillTerminate:(UIApplication *)application;
 (1) - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0);
(2) - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0);

注意###

注册远程通知使用如下方法:

UIRemoteNotificationType type = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:type];
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
        [application registerUserNotificationSettings:setting];
– (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_AVAILABLE_IOS(3_0);
– (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
// 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.
– (void)applicationDidEnterBackground:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
// 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.
– (void)applicationWillEnterForeground:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
上一篇 下一篇

猜你喜欢

热点阅读