1

How to trigger action when the button of location permission alert is pressed? I want to perform segue after allow or cancel button is pressed.

4

1 回答 1

11

我从这个答案和 Apple 的开发者指南中得到了帮助。您可以通过使用NotificationCenter在 Appdelegate 的applicationDidBecomeActive方法上设置观察者来实现它。下面是实现您的任务的代码。

将以下代码放入 ViewController 的 viewDidLoad 中。

NotificationCenter.default.addObserver(self,selector: #selector(doSomeThing), name: .UIApplicationDidBecomeActive, object: nil)

然后,当从Appdelegate调用didBecomeActive时,将调用此函数...因此您可以将要执行的操作放在此函数中

func doSomeThing(){

}

还将以下代码放在同一个 ViewController的viewDidDisappear中以删除观察者,否则您的应用程序将崩溃

 NotificationCenter.default.removeObserver(self,name: .UIApplicationDidBecomeActive,object: nil)
于 2017-03-17T15:39:00.757 回答