How can I exit the App after OpenUrl ?, Does OpenUrl exit the App?

Source: Internet
Author: User

How can I exit the App after OpenUrl ?, Does OpenUrl exit the App?

The SDK does not provide a method to terminate the application. To terminate an application, the only method recommended by Apple is to press the Home button.

However, the Foundation framework integrates the Darwin framework, so that we can use the C function exit (0) to terminate the Application. Of course, this is only for enterprise developers. For individual developers, the only result is that your applications will be rejected by the Apple store.

 

The openUrl method of UIApplication is another way to exit the application. When you call the OpenURL method in the code, your App process is terminated (suspended), and the other App is awakened.

 

Of course, the two withdrawal mechanisms and the final effect are not the same. When you exit the program using exit (0), your App does not only exit the foreground, but also clears the memory occupied by the Program-this is irrecoverable. If the App is Launch again, iOS will read the binary from the disk again-this is a brand new App Image.

 

OpenURL is different. It only suspends your program and is recoverable. Your App only exits from the foreground, but the background still exists. You can "wake up" it at some time, so your App is back, and the application status is still awake. Of course, in case you are not lucky, iOS will also completely recycle your App from the memory, just as exit (0) does-This is generally the case when the system memory is tight.

 

These two methods may coexist in some cases. 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 "Exit" and reclaim all the App memory.

This is a "Paradox ". Because no matter whether exit (0) or openURL, once executed, the operating system will terminate the process execution. If you execute either of the statements, the other statement cannot be executed-because the process has been terminated.

 

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

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

 

[Self defined mselector: @ selector (exitApp) withObject: nil afterDelay: 0.5];

[[UIApplication sharedApplication] openURL:

[NSURLURLWithString: @ "appScheme: //"];

 

The exitApp method is actually a Code such as exit (0 ).

In this way, the two will coexist.

First, let's delay exit (0) by 0.5 seconds and then execute it. Before that, of course, openURL has already been executed.

PerformSelector: The afterDelay method schedules a task to be executed after a certain time. Of course, this time cannot be too long, because iOS allows the app to survive for a period of time after it enters the background, but this time cannot be too long, in this way, after the openURL method is executed, the App is still alive and has the opportunity to execute the scheduled task (exit (0 )).

 

This code works well after iOS 5. But unfortunately, the Swift language is coming.

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 cannot work at all. The cause is unknown. It is a new Bug, but I have not seen any such problem from Apple Radar.

 

After some exploration, I found a way to make the above Code work, that is, to wrap the above Code in the 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 ://")!)

 

})

 

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.