iosswiftpush-notificationswrevealviewcontroller

How to show side menu with swrevealviewController when revealview controller does not exist?


I have a side menu using SwRevealViewController. I have setup with interface builder and is working in the way as desired. This is the design of the storyBoard

Istoryboard

Following the app flow, its always need to pass by the red navigation viewController that handle the menu, the yellow one is the menu view, and the green one is the one view controller which I need to present when a push notification is recieved, no matter the app state. So far i can do that, go to the green view controller without passing by the red one, using this code:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    print("did Recive remote")
    if let messageID = userInfo["gcmMessageIDKey"] {
        print("Message ID: \(messageID)")
    }
    print(userInfo)
    completionHandler()
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "porterDetail")
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
}

After that view is show no navigation controller or even imposible to create a side menu using this code.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "ico_menu"), style: .plain, target: initialViewController, action: #selector(initialViewController.revealToggle(_:)))

But the menu is not shown and nothing happens. any idea how to show the menu in this view to allow user to go out, or what other aproach to get to view and keep navigation.


Solution

  • i weel i at last resolved how to show the side menu comming from the appdelagte when push notification is recived. so the idea is to instancita a navigation controller before the viewcontroller to present to have navigation bar.

    let menuVC = storyboard.instantiateViewController(withIdentifier: "menuVC")
            let chatVC = storyboard.instantiateViewController(withIdentifier: "chatNC")
            ((chatVC as! UINavigationController).topViewController as! newChatViewController).id = id
            let mainRWVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
            mainRWVC.setRear(menuVC, animated: true)
            mainRWVC.setFront(chatVC, animated: true)
            self.window?.rootViewController = mainRWVC
            self.window?.makeKeyAndVisible()
    

    then in the topViewcontroller of the presented navigation. check for id is not nil then present the next view. and then in the presented view. in the did load this code show show the side menu.

     self.navigationItem.leftBarButtonItem = nil
            let revealViewController = self.revealViewController()
            revealViewController?.rearViewRevealWidth = ((UIScreen.main.bounds.width * 74.6875) / 100)
            if revealViewController != nil{
                let button1 = UIBarButtonItem(image: UIImage(named: "ico_menu"), style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:))) // action:#selector(Class.MethodName) for swift 3
                    self.navigationItem.leftBarButtonItem  = button1
                self.navigationItem.leftBarButtonItem?.tintColor = .white
            }
    

    and voila the side menu appears and works as expected.