-2

I am on ChatViewController and i navigated to some other page and again i visited to ChatViewController so how can i check i m on ChatViewController page or not when i visit same page.

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(true)

    UserDefaults.standard.value(forKey: "ChatView")

    print("view will appear called")
}


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)

    UserDefaults.standard.object(forKey: "ChatView")
    print("view will disappear called")
}

Do i need to set any UserDefaults value or some other thing.Could some one help me in this.Thanks in advance

  • your question is not clear to me please elaborate it – Devil Decoder Oct 08 '18 at 06:09
  • when i visit same page. is not able to understand – Anbu.Karthik Oct 08 '18 at 06:10
  • 1
    What would be the point of checking if your view controller is your view controller inside your view controller? (Yes, this is what you question sounded like to me @_@) – Rakesha Shastri Oct 08 '18 at 06:14
  • Possible duplicate of [Get top most UIViewController](https://stackoverflow.com/questions/26667009/get-top-most-uiviewcontroller) – Pankil Oct 08 '18 at 06:38
  • When you create that object or use appropriate storyboard segue, and present it to the user, you will be in same view controller. Why do you want to check? – Satyam Oct 08 '18 at 06:51
  • i have implemented push notifications in my app and when i click i have to navigation to chat page based on push notification and particular user who sends chat message to current user.So i m trying to notifity that i m on chat page or not when i click some other page and navagate to ChatViewController – ashwani5389 Oct 08 '18 at 07:25

3 Answers3

0

Swift 5 and iOS 15

     if self.navigationController?.presentedViewController != nil && 
    self.navigationController?.presentedViewController is ChatViewController {
        return
}
logeshpalani31
  • 1,416
  • 12
  • 33
-1

You can check the navigation stack of your current navigation controller.

let viewControllers = self.navigationController!.viewControllers as [UIViewController]
for vc:UIViewController in viewControllers {
    if vc.isKind(of: YourVC.self) {
    }
}
ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
-2

It would make no sense to use user defaults because those will be persistent even if your app is killed. That means if user is on chat, closes the app, opens it again your user defaults value will say user is still on the chat screen.

There are multiple ways of showing your chat screen. It might be presented, might be pushed in navigation bar, inside tab bar or even custom added to content view. So I would say it is still best to use willAppear and didDisappear. A static value should do the trick:

class ChatViewController: UIViewController {

    static private(set) var currentVisibleInstance: ChatViewController?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        ChatViewController.currentVisibleInstance = self
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        ChatViewController.currentVisibleInstance = nil
    }

}

Now you can use this from anywhere:

let isChatVisible: Bool = ChatViewController.currentVisibleInstance != nil
let currentChatController: ChatViewController? = ChatViewController.currentVisibleInstance

Naturally you could put this currentVisibleInstance in some other class if you see fit. You would simply change to SomeOtherClass.currentVisibleChatViewController = self.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • i have implemented push notifications in my app and when i click i have to navigation to chat page based on push notification and particular user who sends chat message to current user.So i m trying to notifity that i m on chat page or not when i click some other page and navagate to ChatViewController – ashwani5389 Oct 08 '18 at 07:16
  • @ashwani5389 Maybe it would be better to put that into your question. What you are asking here is way beyond what you have asked in this post. It is best if you create a new post adding all of information you have along with what you have tried and what is your hierarchy of your view controllers. – Matic Oblak Oct 08 '18 at 07:32