Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to terminate an app programmatically in iOS 12

Tags:

ios

swift

ios12

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.

like image 378
Louis Legout Avatar asked Jun 11 '18 13:06

Louis Legout


People also ask

How do I programmatically quit my iOS application?

An iOS app never displays a Close or Quit option. People stop using an app when they switch to another app, return to the Home screen, or put their devices in sleep mode. Never quit an iOS app programmatically.

Which method is called when app is killed iOS?

Tells the delegate when the app is about to terminate. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.0+ tvOS 9.0+


1 Answers

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.

like image 59
ishwardgret Avatar answered Sep 19 '22 11:09

ishwardgret