2

I'm working on an iOS framework. I have a requirement to log events when user enters or exits a particular View Controller. For that I was thinking if somehow I could be able to register a notification to trigger a custom method when the root view controller changes. Or perhaps use KVO. But I don't understand how to do this from an implementation point of view since I cannot find any such notification.

Any help in this regard would be highly appreciated. Thanks.

Please note that this is a framework project. So the framework is built and then added/embedded into another app. I don't have any information about the view controllers in that app. The only thing I can access is UIWindow's root view controller. So, I need to know when a change occurs in it.

Asad Javed
  • 273
  • 4
  • 17
  • Log enteries in `viewDidAppear` and `viewWillDisappear` methods of that VC. Alternatively you can use `viewWillAppear` and `viewWillDisappear`. See more in [UIViewController ClassReference](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/viewDidDisappear:) – NSNoob Mar 21 '16 at 07:07
  • 1
    As I've told that it is a Framework project. So I don't know in advance anything. Anybody could just embed my framework in their app and use it. How would I access the view controllers methods. I don't think so this is possible. If so, please tell how – Asad Javed Mar 21 '16 at 07:09
  • 1
    Ah I misread your question. Yup that method won't work for you. I am not sure about NSNotification either. Let me look something up. If i find anything suitable, I will get back to you. – NSNoob Mar 21 '16 at 07:12
  • Sure please. I'll be waiting for a reply... – Asad Javed Mar 21 '16 at 07:14
  • By the way... I tried looking into KVO in swift. But the KVO implementation in swift requires the properties you want to observe be **dynamic** and **KVO compliant**. The **RootViewController** property of **UIWindow** doesn't appear to be dynamic and I don't know either if it's **KVO compliant** or not. Some suggest subclassing UIWindow but I want to keep things simple. – Asad Javed Mar 21 '16 at 07:16

2 Answers2

2

Got it working. The answer was method swizzling. Not recommended as a first solution to the problem. But if used carefully and you know exactly what you are doing that it is the way to go.

Found a very useful article here: Method Swizzling

Asad Javed
  • 273
  • 4
  • 17
0

You can override UIViewController's viewWillAppear and viewWillDisappear to know when a view controller is about to be presented/dismissed.

Alternatively you can use viewDidAppear and viewDidDisappear.

If you want to do the logging on users behalf then you really have two options:

1. provide a base UIViewController sub-class for them to override, which implements a required logic in viewWillAppear/viewWillDisappear methods.

2. implement convenience methods (like logAppearEvent, logDisappearEvent) for them to call manually on their own in their UIViewController subclasses.

damirstuhec
  • 6,069
  • 1
  • 22
  • 39
  • It would be better if you add coding example to it. Otherwise it should have been a comment, as I left one. – NSNoob Mar 21 '16 at 07:09
  • @NSNoob What would that look like? An empty method declarations? Not all answers need code snippets. ;) – damirstuhec Mar 21 '16 at 07:10