当我们退出应用程序而不杀死应用程序Swift 4时,弹出窗口不会重新出现

时间:2018-06-04 13:50:45

标签: ios swift xcode popup

早上好我有问题,当我进入我的应用程序时会出现一个带有func viewDidAppear()的弹出窗口,但是当我点击" No"退出应用程序的按钮,就像我们按下主页按钮一样。当我重新进入应用程序而不杀死它时,弹出窗口不再出现......

我搜索了3个小时,但我找不到解决方案......

以下是代码:



 override func viewDidAppear(_ animated: Bool) {
        if warning == true{
            self.alertStart()
        }
    }
    
    func alertStart(){
        let alertatstart = UIAlertController(title: "ATTENTION !", message: "La version de cette application est en cours de bêta, de nombreux bug sont à déclarer, souhaitez-vous tout de même continuer ?", preferredStyle: .alert)
        alertatstart.addAction(UIAlertAction(title: "Oui", style: .destructive, handler:{ (UIAlertAction) in
            self.warning = false
            if self.betaKeyAlreadyConfirmed == true {
                
            }else{
               self.askDevMail()
            }
        }))
        alertatstart.addAction(UIAlertAction(title: "Non", style: .cancel, handler: { (UIAlertAction) in
            self.warning = true
            UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
        }))
        self.present(alertatstart, animated: true)
    }
    




我已经尝试将func放入viewDidLoad()但它不起作用......

我希望你能帮助我,非常感谢你,如果你需要更多的信息,请在答案中告诉我

路易斯。

3 个答案:

答案 0 :(得分:5)

ViewDidAppear在您按Home键并再次返回时不会被调用。

使用UIApplicationDidBecomeActiveUIApplicationWillResignActive的通知:

ViewDidLoad

NotificationCenter.default.addObserver(self, selector: #selector(self.callBackForActiveNotification), name: Notification.Name.UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.callBackForBackgroundNotification), name: Notification.Name.UIApplicationWillResignActive, object: nil)

添加功能:

@objc func callBackForBackgroundNotification(){
   // This will be called when your app goes in background
}

@objc func callBackForActiveNotification(){
   // This will be called when your app become active, show pop up here again if it is not shown
}

答案 1 :(得分:0)

请在 ViewWillAppear AppDelegate 级别尝试。 在 AppDelegate 级别,它应该可以正常工作。

当您点击主页按钮时,应用会转到后台模式。否则,如果再次打开应用程序,应用程序将进入前台模式。

在appdeletegate.m(appdelegate.swift-Swift)文件中,您可以看到以下功能。

applicationDidBecomeActive和applicationWillDidEnterBackground。你可以把你的代码放在那里。

这是官方document

答案 2 :(得分:0)

override func viewDidLoad() {
    super.viewDidLoad()   
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func keyboardWillShow(notification: NSNotification) {
     print("keyboardWillShow")
}

func keyboardWillHide(notification: NSNotification){
     print("keyboardWillHide")
}