Processing after IOS development (103)

Source: Internet
Author: User

1 Preface
IOS4 provides background processing to run applications in the background. In some cases, you can even run applications in the background after you press the Home button.

2. Details
In IOS, you can click the Home button to pause the application. This pause state is similar in concept to setting Mac to sleep mode. All the application's working memory is in RAM, and it is not executed at all when it is paused. Therefore, switching back to such an application is very fast. The system provides multiple methods to notify the application of changes in its execution status through the UIApplication class. This class provides many delegate methods and notifications for this purpose, and we will introduce how to use them.

2.1 application declaration cycle
2.1.1 not running
This status indicates that all applications are on a device that has just restarted. when the device is on, no matter when the application starts, the application returns to the not running status only in the following situations:

(1) The Info. plist of the application contains the UIApplicationExitsOnSuspend key set to YES;

(2) The application has been suspended and the system needs to know some memory;

(3) The application crashes during running.

2.1.2 Activity
This is the normal running status of the application displayed on the screen. It can receive user input and update the display.

2.1.3 background
In this status, the application gets a certain amount of time to execute some code, but it cannot directly access the screen or get any user input. Shortly after the user presses the Home button, all applications will enter the state, and most of them will quickly enter the paused state. The application you want to run in the background remains in this status until it is activated.

2.1.4 pause
The suspended application is frozen. Normal applications will soon change to this state in the background. All memory used by this application during activity will be retained without understanding it. If the user switches the application back to the active state, it will return to the previous state. On the other hand, if the system needs to provide more memory for the currently active application, any suspended application may be frozen (and return to the not running state ), their memory will be released for other purposes.

2.1.5 no activity
The application is inactive only during the critical overuse between two other States. The only premise that an application can be inactive for any long period of time is that the user is processing system prompts (such as incoming call or SMS prompts) or the user has locked the screen. This is basically an intermediate excessive state.

2.2 Status Change Notification
To manage changes between these statuses, UIApplication defines some methods that can be implemented by its delegate. In addition to the delegate method, UIApplication also defines a set of matched notification names. This allows other objects except the application delegate to register notifications when the application state changes.

How to track the application execution status and delegate the corresponding notification Name:

Delegate method notification name

// You can add some application initialization code in application: didfinishlaunchingwitexceptions.


Application: didfinishlaunchingwitexceptions: UIApplicationDidFinishLaunchingNotification

// If you press the home button, applicationWillResignActive is called. In applicationWillResignActive, assuming that the application is going to enter the background state, it is only a critical state. It will eventually be restored to the active state.


ApplicationWillResignActive: UIApplicationWillResignActiveNotification

// If you switch the application back to the foreground, applicationDidBecomeActive will be called.


ApplicationDidBecomeActive: UIApplicationDidBecomeActiveNotification

// Release all resources that may be created later, save all user data, and disable network connections. If it takes too long (more than 5 seconds), the system determines that the application is abnormal and terminates it. It should implement applicationWillEnterForeground: to recreate the content destroyed in applicationDidEnterBackground.


ApplicationDidEnterBackground: UIApplicationDidEnterBackgroundNotification

ApplicationWillEnterForeground: UIApplicationWillEnterForegroundNotification

// This method is rarely used. It is called only when the application enters the background and the system decides to skip the pause state and terminate the application for some reason.

ApplicationWillTerminate: UIApplicationWillTerminateNotification

2.3 instance resolution
Next we will create a project to really observe the call time of these methods:


ZYAppDelegate. m:


[Plain]
//
// ZYAppDelegate. m
// State Lab
//
// Created by zhangyuc on 13-6-8.
// Copyright (c) 2013 zhangyuc. All rights reserved.
//
 
# Import "ZYAppDelegate. h"
 
# Import "ZYViewController. h"
 
@ Implementation ZYAppDelegate
 
-(Void) dealloc
{
[_ Window release];
[_ ViewController release];
[Super dealloc];
}
 
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
/**
* _ Cmd: Objective-C provides a convenient built-in variable named _ cmd, which always contains the selector of the current method.
* NSStringFromSelector (): NSString representation that the function returns to the selector.
* The combination of the two methods provides a shortcut for outputting the name of the current method. You do not need to retype or copy or paste it.
*/
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
Self. viewController = [[[ZYViewController alloc] initWithNibName: @ "ZYViewController" bundle: nil] autorelease];
Self. window. rootViewController = self. viewController;
[Self. window makeKeyAndVisible];
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 shocould use this method to pause the game.
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
}
 
-(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 (@ "% @", NSStringFromSelector (_ cmd ));
}
 
-(Void) applicationWillEnterForeground :( UIApplication *) application
{
// Called as part of the transition from the background to the inactive state; here you can undo changes of the changes made on entering the background.
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
}
 
-(Void) applicationDidBecomeActive :( UIApplication *) application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previusly in the background, optionally refresh the user interface.
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
}
 
-(Void) applicationWillTerminate :( UIApplication *) application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground :.
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
}
 
@ End

//
// ZYAppDelegate. m
// State Lab
//
// Created by zhangyuc on 13-6-8.
// Copyright (c) 2013 zhangyuc. All rights reserved.
//

# Import "ZYAppDelegate. h"

# Import "ZYViewController. h"

@ Implementation ZYAppDelegate

-(Void) dealloc
{
[_ Window release];
[_ ViewController release];
[Super dealloc];
}

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
/**
* _ Cmd: Objective-C provides a convenient built-in variable named _ cmd, which always contains the selector of the current method.
* NSStringFromSelector (): NSString representation that the function returns to the selector.
* The combination of the two methods provides a shortcut for outputting the name of the current method. You do not need to retype or copy or paste it.
*/
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
Self. viewController = [[[ZYViewController alloc] initWithNibName: @ "ZYViewController" bundle: nil] autorelease];
Self. window. rootViewController = self. viewController;
[Self. window makeKeyAndVisible];
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 shocould use this method to pause the game.
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
}

-(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 (@ "% @", NSStringFromSelector (_ cmd ));
}

-(Void) applicationWillEnterForeground :( UIApplication *) application
{
// Called as part of the transition from the background to the inactive state; here you can undo changes of the changes made on entering the background.
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
}

-(Void) applicationDidBecomeActive :( UIApplication *) application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previusly in the background, optionally refresh the user interface.
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
}

-(Void) applicationWillTerminate :( UIApplication *) application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground :.
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
}

@ End
Running result (console ):

Just entered the program:


10:29:17. 243 State Lab [1866: c07] application: didfinishlaunchingwitexceptions:

10:29:17. 248 State Lab [1866: c07] applicationDidBecomeActive:

Click Home:
Return to the project again:
The last situation is very interesting. First, the program is quickly activated here, then becomes inactive, and finally enters the background.
These entrusting methods and notifications are directly related to a certain "running" status: activity, no activity, and background. Each delegate method is called only in one State (each notification only appears in one State ). The most important status is between the active status and other statuses. Some excessive statuses (such as from the background to the paused status) will not receive any notifications.


10:29:48. 598 State Lab [1866: c07] applicationWillResignActive:

10:29:48. 599 State Lab [1866: c07] applicationDidEnterBackground:


10:30:11. 357 State Lab [1866: c07] applicationWillEnterForeground:

10:30:11. 358 State Lab [1866: c07] applicationDidBecomeActive:

If the mobile phone receives an SMS message:

10:29:48. 598 State Lab [1866: c07] applicationWillResignActive:


If the message is disabled:

10:29:17. 248 State Lab [1866: c07] applicationDidBecomeActive:


If you reply to an SMS message:

10:29:17. 248 State Lab [1866: c07] applicationDidBecomeActive:


10:29:48. 598 State Lab [1866: c07] applicationWillResignActive:

10:29:48. 599 State Lab [1866: c07] applicationDidEnterBackground:

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.