Attempt to present UIAlertController whose view is not in the window hierarchy with localnotification [duplicate]

谁说胖子不能爱 提交于 2020-01-17 05:34:07

问题


I am trying to present AlertView once the user clicks on the local notification. The AlertView has options of cancel or ok.

extension ViewController:UNUserNotificationCenterDelegate{


    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        print("Tapped in notification")
        if(defaults.object(forKey: "alertOn") != nil){


            // Create the alert controller
            let alertController = UIAlertController(title: "Some text", message: "Some text again", preferredStyle: .alert)


            // Create the actions
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                UIAlertAction in

            }

            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                UIAlertAction in
            }

            // Add the actions
            alertController.addAction(okAction)
            alertController.addAction(cancelAction)

            // Present the controller
                self.present(alertController, animated: true, completion: nil)

        }
    }


func triggerNotification(){

        print("notification will be triggered in five seconds..Hold on tight")
        let content = UNMutableNotificationContent()
        content.title = "SomeText"
        content.body = "Some more text"
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
        let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)

        UNUserNotificationCenter.current().delegate = self as! UNUserNotificationCenterDelegate
        UNUserNotificationCenter.current().add(request){(error) in

            if (error != nil){

                print(error?.localizedDescription)
            }
        }
    }

It should show me the alertview with option to ok or cancel a request. Instead its showing me message UIAlertController whose view is not in the window hierarchy

When I put the alertview in viewdidapear it works fine but when I put it in userNotificationCenter my AlertView not get attached to the main view.

Brief View of Code

extension ViewController:UNUserNotificationCenterDelegate{
          func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

         Present UIAlertView

        }
}

ViewController{

Call to notification when app is in background 
triggerNotification()

triggerNotification(){
 Definition 
}
}

回答1:


try this

func currentTopViewController() -> UIViewController {
    var topVC: UIViewController? = UIApplication.shared.delegate?.window?.rootViewController
    while topVC?.presentedViewController {
        topVC = topVC?.presentedViewController
    }
    return topVC!
}

and present the VC as

let currentTopVC: UIViewController? = self.currentTopViewController()

currentTopVC.present(alertController, animated: true, completion: nil)


来源:https://stackoverflow.com/questions/42218664/attempt-to-present-uialertcontroller-whose-view-is-not-in-the-window-hierarchy-w

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!