How do I really quit the app after OpenURL?

Source: Internet
Author: User
Tags gcd

The SDK does not provide a way to terminate the application. To terminate the application, the only way Apple recommends is to press the home button.

But the foundation framework integrates the Darwin framework so that we can use the C function exit (0) to terminate application. Of course, this is only for business developers. For individual developers, the only result of this is that your app will be rejected by the Apple store.

UIApplication's OpenURL method is another way to exit the application. When you call the OpenURL method in your code, your app process is terminated (suspended) and the other app is awakened.

Of course, the two exit apps are not the same mechanism and end result. When you exit the program using exit (0), your app does not just exit the foreground, the memory used by the program is cleared-it's unrecoverable. If launch again this app,ios will re-read binary from disk-this is a brand new app image.

OpenURL is different, it just hangs up your program, which is recoverable-your app just quits from the foreground, but still exists in the background. The user can "wake up" it at some point, and your app is back, and the state of the application still wakes up. Of course, if you're unlucky, iOS will completely reclaim your app from memory, as it did in exit (0)-Usually when the system is in a tight memory.

Both of these methods may need to coexist at some point. For example, we want to wake up another app, such as Safari, before the app exits. At the same time we want our app to be the real "exit" and recycle all of the app's memory.

This is a "paradox". Because regardless of exit (0) or OpenURL, once executed, the operating system terminates the execution of the process. As long as you execute any of these statements, the other statement cannot be executed-because the process has been terminated.

But in some cases, this paradox is true through the ingenious use of the multitasking mechanism of iOS.

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, we let exit (0) delay for 0.5 seconds before execution, and before that OpenURL of course executed.

The Performselector:afterdelay method dispatches a task to execute after a certain time. Of course, this time can not be too long, because iOS allows the app to enter the background still have a "survival" time, but this time can not be too long, so that after the execution of the following OpenURL method, the app is still alive, there is a chance to perform the scheduled task (that is, exit (0)).

This code works well after iOS 5. But unfortunately, the swift language comes.

In Swift, the Performselector method no longer exists.

Of course, we immediately thought of another alternative, GCD:

var dispatchtime:dispatch_time_t =dispatch_time (

Dispatch_time_now,int64 (0.5 * Double (NSEC_PER_SEC)))

Dispatch_after (Dispatchtime,

Dispatch_get_global_queue (dispatch_queue_priority_background,0),

{

Exit (0)

})

Uiapplication.sharedapplication (). OpenURL (Nsurl (string: "appscheme://")!)

However, this code does not work at all. The reason is unknown, it can be a new bug, but I have not seen anyone radar this problem to Apple so far.

After some exploration, I found the way to get the code to work by wrapping the above code in a new GCD asynchronous block:

Dispatch_async (Dispatch_get_main_queue (), {()->voidin

var dispatchtime:dispatch_time_t = dispatch_time (Dispatch_time_now,int64 (0.5 * Double (NSEC_PER_SEC)))

Dispatch_after (Dispatchtime, Dispatch_get_global_queue (dispatch_queue_priority_background,0), {

Exit (0)

})

Uiapplication.sharedapplication (). OpenURL (Nsurl (string: "appscheme://")!)

})

How do I really quit the app after OpenURL?

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.