[Code Note] runs in the background. You can select either the foreground or background or the foreground background.

Source: Internet
Author: User

[Code Note] runs in the background. You can select either the foreground or background or the foreground background.

1. Engineering Drawing.

Ii. Code.

AppDelegate. h

AppDelegate. m

RootViewController. h

#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier backgroundTaskIdentifier;@property (nonatomic, strong) NSTimer *myTimer;@end

 

RootViewController. m

# Import "AppDelegate. h "# import" RootViewController. h "@ implementation AppDelegate-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; // Override point for customization after application launch. rootViewController * rootVC = [[RootViewController alloc] init]; UINavigat IonController * nav = [[UINavigationController alloc] initWithRootViewController: rootVC]; self. window. rootViewController = nav; self. window. backgroundColor = [UIColor whiteColor]; [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 te Mporary 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 .} -(void) applicationDidEnterBackground :( UIApplication *) application {// use this method to release public resources and storage User Data, stop our defined timer (timers), and store related information before program termination. // If our application provides the background execution method, this method will replace applicationWillTerminate when the program exits. // Marking a long-running background task will start. // After debugging, it is found that iOS gave us an additional 10 minutes (600 s) to execute this task. Self. backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler: ^ (void) {// when the time the application leaves for the background is about to end (the time the application leaves for background execution is limited ), this Block will be executed // we need to perform some cleanup work in the Block. // If the cleanup fails, the program will be suspended. // The cleanup should be performed in synchronous mode in the main thread [self endBackgroundTask];}]; // simulate a Long-Running Task self. myTimer = [NSTimer failed: 1.0f target: self selector: @ selector (timerMethod :) userInfo: nil repeats: YES];}-(void) endBackgroundTask {dispatch_queue_t mainQueue = Queue (); appDelegate * weakSelf = self; dispatch_async (mainQueue, ^ (void) {AppDelegate * str OngSelf = weakSelf; if (strongSelf! = Nil) {[strongSelf. myTimer invalidate]; // stop the timer // each call to the beginBackgroundTaskWithExpirationHandler: method must call the endBackgroundTask method accordingly. In this way, you can tell the application that you have completed the execution. // That is to say, if we want more time to complete a task for iOS, we must tell iOS when you can complete the task. // Tell the application: "Please pay back the money. // Mark the specified background task to complete [[UIApplication sharedApplication] endBackgroundTask: self. backgroundTaskIdentifier]; // destroy the background task identifier strongSelf. backgroundTaskIdentifier = UIBackgroundTaskInvalid ;}}); // simulate a Long-Running Task method-(void) timerMethod :( NSTimer *) paramSender {// The backgroundTimeRemaining attribute contains the time left by the program. NSTimeInterval backgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining]; If (backgroundTimeRemaining = DBL_MAX) {// print NSLog at the front end (@ "Background Time Remaining = Undetermined ");} else {// NSLog (@ "Background Time Remaining = %. 02f Seconds ", backgroundTimeRemaining) ;}}-(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 t He background. // Add this code segment and it will not run on the foreground. Otherwise, it will run together in the background before the meeting. In addition to the first startup, The frontend is not running, and the backend is running. If (self. backgroundTaskIdentifier! = UIBackgroundTaskInvalid) {[self endBackgroundTask] ;}}- (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 .} -(void) applicationWillTerminate :( UIApplication *) application {// Called when the application is about to terminate. save data if appropriate. see also applicationDidEnterBackground :.} @ end

 

 

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.