How do I wake up with an app before exiting the app?

Source: Internet
Author: User

Shimen the main contribution, respect the work of the author, please do not reprint.

If the article is helpful to you, you are welcome to donate to the author, support the Shimen, donate any amount, ^_^

I want to donate: Click Donate

Cocos2d-x source code Download: Dot I teleport


The SDK does not provide a way to terminate the application.

To terminate the application, the only way Apple recommends is to press Homebutton.


But the foundation framework integrates the Darwin framework so that we can use the C function exit (0) to terminate application.

The OpenURL method for UIApplication is another way to exit the application.

When you call the OpenURL method in your code, your app's process is terminated (suspended), and an app is awakened.

Of course, the two exit app mechanism and finally effect is not the same.

When you exit the program using exit (0), your app doesn't just exit the foreground and the memory used by the program is cleared-it's unrecoverable.

Assuming again launch this app,ios will again read the binary from disk-this is a brand new app image.

OpenURL are different. It just hangs up your program, which is recoverable.

Your app exits from the foreground but still exists in the background.

The user is able to "wake" it at some point, and your app is back, and the state of the application still wakes up.

Of course, if your luck is not good, iOS will completely recycle your app from memory,

As the exit (0) did. This is usually the time when system memory is tight.

These two methods may need to coexist at some point.

For example, we want to before the app exits. Wake up with an app. For example, Safari.

At the same time we want our app to be the real "exit" and reclaim all of the app's memory.


This is a "paradox". Because regardless of exit (0) or OpenURL, the operating system terminates the running of the process once it has been run.

Just run any of the statements in both. Another statement cannot be run-because the process has been terminated.

But in some cases, the smart use of the iOS multitasking mechanism. This paradox is true.

For example, we can use the following O-C code to achieve this purpose:

[Self performselector: @selector (Exitapp) Withobject:nil afterdelay:0.5];

[[UIApplication Sharedapplication]openurl:

[nsurlurlwithstring:@ "appscheme://"];

The Exitapp method is actually a code exit (0).

So they both coexist.


First of all. We let exit (0) delay for 0.5 seconds before running, but before that OpenURL of course ran out.

The Performselector:afterdelay method dispatches a task to run after a certain time.

Of course, this time cannot be too long, ".

Since iOS agrees that the app still has a "live" time after entering the background, the time is not too long,

This means that after the OpenURL method is running, the app is still alive and has the opportunity to run the scheduled task (that is, exit (0)).


Shimen the main contribution of the Lord, respect the author's labor results. Please do not reprint.

If the article is helpful to you, you are welcome to donate to the author and support the Shimen master. The donation amount is arbitrary, heavy in the mind ^_^

I want to donate: Click Donate

Cocos2d-x source code Download: Dot I teleport

How do I wake up with an app before exiting the app?

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.