使用 UIAlertController 退出应用程序

Exit application using a UIAlertController

如果用户在没有互联网连接的情况下打开应用程序,会弹出一个 window 提示需要连接,并且有一个确定按钮。我想要确定按钮退出应用程序。这是我拥有的:

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)
}

我将使用它退出应用程序:

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

就是不知道怎么连接到上面的确定按钮。

不要。苹果会拒绝这个(如果他们看到了)。

只需通知用户并添加一个'retry'按钮。重试按钮显然应该再次检查连接。

要真正回答这个问题,您目前已经在按钮操作上设置了 handler: nil,您实际上可以设置一个处理程序并使用它来调用您喜欢的任何逻辑。

你可以通过下面的代码来处理当用户按下OK的时候

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!!")
}))