user3483203
user3483203

Reputation: 51155

Exit application using a UIAlertController

If a user opens up the application without an internet connection, a window pops up that says a connection is required, and there is an ok button. I want to the ok button to exit the application. Here is what I have:

if !isConnectedToNetwork(){
    let alert = UIAlertController(title: "No Internet", message: "You need an internet connection to use this app", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

I am going to use this to exit the app:

UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)

I just don't know how to connect it to the OK button above.

Upvotes: 0

Views: 1961

Answers (2)

benmore99
benmore99

Reputation: 933

You can handle when user press OK by the following code

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,
handler: { (action:UIAlertAction!) -> Void in
   //after user press ok, the following code will be execute
   NSLog("User pressed OK!!")
}))

Upvotes: 2

Wain
Wain

Reputation: 119031

Don't. Apple will reject this (if they see it).

Simply inform the user and add a 'retry' button. The retry button should obviously check the connection again.

To actually answer the question, you have currently set the handler: nil on the button action, instead you can actually set a handler and use it to call whatever logic you like.

Upvotes: 4

Related Questions