How to terminate an app programmatically in iOS 12
Asked Answered
R

3

16

I have a problem I put my iPhone 6 on iOS 12 beta 1 and that's so a method in my application to close when press on a popup button doesn't works on iOS 12 but works on iOS 11.4.1

That's the method that I used:

UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)

In iOS 11 the app exit properly, in iOS 12 the app doesn't exit just do nothing when press on the popup button.

I know that this is Swift 4.2 and when I rode the news and modifications I haven't seen something like a link with a new method to close app properly.

I need that because I use that to close the app if the person doesn't accept Terms and conditions.

Rajewski answered 11/6, 2018 at 13:39 Comment(7)
You can call exit if I recall correctlyCleora
exit() should work but it's not recommended at all. Just leave the person at the welcome/login screen if they doesn't accept the terms.Hellbender
exit is depreacted because exit appears to be a crashRajewski
Technically, an app terminating itself is a crash no matter how you accomplish it and it will likely result in your app being rejected by Apple.Anabaptist
evene exit() is use in Obj-C not in Swift... I'm lost I need because if the app doesn't quit when somebody tries to bypass the terms and conditions... It's complicate, I get the datas of the persons who's accepts and disagrees terms and conditions if somebody works to bypass I will not be able to check that out of that quit.Rajewski
@LouisLegout Well you'll have to clean that data if the user declines the terms.Hellbender
You are not allowed to call exit(). Its forbidden in the Apple HIG document. As to what to do if your user doesnt accept the option? Don't worry, your not allowed to present a license screen on start up either. That situation will never occur in an accepted app.Octodecillion
A
22

You can call exit method

exit(-1)

or you can use NSXPCConnection.suspend

UIControl().sendAction(#selector(NSXPCConnection.suspend),
                       to: UIApplication.shared, for: nil)

Besides Apple isn't recommending to force terminate your app. It doesn't matter how you do that.

Check this post.

Album answered 11/6, 2018 at 13:52 Comment(4)
doesn't work UIControl :'( on iOS12 lot of changes has been appliedRajewski
exit don't work and even it will work I will never use it and UIControl doesn't work :(Rajewski
@LouisLegout can you add 'UIApplicationExitsOnSuspend' property to 'true' in your plist and try again ? If it's still not working then it's a iOS 12 issue and we'll learn this by time.Album
Instead of exiting app, try simulating the restart scenario: https://mcmap.net/q/747634/-how-can-i-restart-an-application-programmatically-in-swift-on-iosChromatic
S
21

Try this code :

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
    UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
     DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
      exit(0)
     }
}

This will wait 1 sec and app will take 0.5 second to close. You can dim display before this code snippet.

Stitt answered 26/9, 2018 at 10:26 Comment(2)
Did someone upload an app to the appStore with this code? will apple reject my app for doing this?.Hypothec
Instead of exiting app, try simulating the restart scenario: https://mcmap.net/q/747634/-how-can-i-restart-an-application-programmatically-in-swift-on-iosChromatic
B
4

This would be the easiest way to terminate your App if you don‘t mind triggering a crash error.

// Create an array and "index out of range"
[0][1]
Bronchopneumonia answered 12/7, 2021 at 3:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.