0

I need to track the users navigation flow and store it in a Dictionary, Every time user visits any screen in my app. I can see the navigation stack in my console output by using code

navigationController?.viewControllers

But don't know, how to store this in dictionary.

Please help

7
  • 1
    What is the purpose of using dict.?
    – Mahendra
    May 15, 2019 at 9:05
  • 1
    do you need to get the name of the vc for each 1 ? May 15, 2019 at 9:08
  • @Sh_Khan:Yes I need the name of every view controller
    – Vork
    May 15, 2019 at 9:13
  • @MahendraGP :Actually the purpose of capturing the name of VC is for storing users navigation in a txt file and send to server.For that I will initially add th flow indictionary and then i will write this to a text file
    – Vork
    May 15, 2019 at 9:15
  • 3
    Maybe you could add a small example to your question like if the user visits vc1 then goes to vc2 and back again to vc1, what do you expect to have in your dictionary then? May 15, 2019 at 9:15

3 Answers 3

0

You can get the names of all UIViewControllers on navigationStack using,

let controllers = self.navigationController?.viewControllers.compactMap({ String(describing: type(of: $0.self)) }).joined(separator: "_")
print(controllers) //Output: "VC1_VC2_VC3"

You can store the controllers string in your file.

0

Hope this helps to get viewcontroller. Follow this and store into global dictionary using singleton.

Global Function:

public func storeCurrentViewController(_ currentFile: String = #file)
{
    let className = currentFile.components(separatedBy: "/").last ?? ""
    let classNameArr = className.components(separatedBy: ".")
    print("\n\n--> CurrentViewController:  \(classNameArr[0])")

    //*********** Use dictionary and do your logics

}

Function Call:

override func viewDidLoad() {
    super.viewDidLoad()
    storeCurrentViewController()
}
1
  • Hope this helps. Store into dictionary as you required. May 15, 2019 at 9:31
0
NSArray *viewControllers = [[self navigationController] viewControllers];
                for( int i=0;i<[viewControllers count];i++){
                    id obj=[viewControllers objectAtIndex:i];    
                    NSLog(@"%@",[obj classNameForClass:[obj class]]);
}

Not sure if this works. Give a try...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.