Tracking/Instrumenting events/methods fired - ios

I am trying to print out (e.g., NSLog) the information about events that fired on iPhone application. For example user executes a scenario and I want to track all the methods called and events clicked by the user.
Is there anyway to do that using a category extension and method swizzling features of objective-C to inject some code to log and print the information needed? Right now, I defined a category for UIWindow, UIApplication and UIView. I am not sure where is the best place to track tho, e.g., [UIWindow sendEvent:]? Should I observe objc_msgSend()?
I basically want to instrument a project and inject code with category extension in order to not change the source code as much as possible.

Related

iOS How to differentiate between changes to underlying data from outside vs from within the class

I am having a conceptual issue which I am not sure there is a solution for - tracing the origin of data change.
For example, in my app I have a preferences class that writes settings to user defaults, and I have set up a custom observer pattern where any class interested in a change to settings subscribes as an observer, and is notified when the setting changes. Now, if a setting changes online or on another device, the app receives the change, saves it and notifies all observers.
If I make the change within the app by going into the settings and selecting a new value, the settings class receives the change and again notifies all observers.
The problem is, that the way one would handle the change changes based on whether is the change within the class or elsewhere.
For example, let's say I let the user pick languages they would like to support. This is done through a UITableView where the user can add/remove/reorder the languages. The class that handles this is subscribed as an observer of changes to the languages property of settings, so that if I am in the page and data is changed online, the table view gets reloaded and new languages are displayed. However, when I drag the languages into a new order, I save the new order, this fires the change event of which the class is an observer, and so the table view reloads, cutting short the animation of the drag.
In short, when receiving the observer event, I would like to be able to know whether has this change been handled already (such as if it's internal and so I already dragged/inserted/removed a row) or it requires a table view reload.
Is there any pattern, concept or method that I am unaware of which addresses this?
(By the way I am writing this in Swift 4 for a universal iOS app if that makes any difference)
Thanks

What is best approach in iOS for message passing, NSNotification or Delegation?

I am new to iOS. I have come to know about these two approaches for message passing but I am unable to choose between them.
Conceptually, a delegate is a helper or an object that does part of the work for some other object that it can't do by itself. Frequently there will be a protocol involved and the object that has a reference to the delegate expects it to behave in a predefined way or, at least, in a way that's specific to the needs of the calling class.
Example: All the methods defined for a UITableViewDelegate are specific to table activity.
Notifications are more about state changes. The object sending the notification doesn't need help to do its work, it just lets other objects know about a change in case they want to react. If nothing else cares about the change, that's OK.
Example: An object that receives a UIApplicationDidBecomeActiveNotification may do whatever the developer needs at that time, not just things related to the UIApplication.
Delegate Patterns are mostly used because of delegate Object knows in which class delegate methods are implement. Delegate Object contains the references for object of that Class where you want to perform you task.
In Notification Patterns , simply a notification is broadcast over the entire classes and each class is search for the observer methods implementations So it took more time to search observer methods as compare to Delegate pattern.

iOS - Correct way to "listen" for a delegate method from within a class

I've just implemented the CLLocationManager in my AppDelegate, which is currently printing the location whenever it's updated.
What I'm trying to do now is trigger a method in one of my view controllers whenever that location is updated.
I've done something similar before using protocol, but I'm not sure if that's possible with the AppDelegate.
I'm working in Swift, but an Objective-C response would also be helpful as I'm looking for the logic behind how to achieve this.
You can post notifications to NSNotificationCenter from your application delegate (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/) and subscribe to in in your controller (do not forget to unsubscribe in dealloc). Just make sure you do not update any GUI when application is in background (you can check state directly in controller).

How to use the data from a notification that is meant to be used in VC A , when i am in VC B

Ok so i have been trying to figure out how to do this for a while but i did not seem to find way to do it. Also i would like the proper way to do this.
A server that i have is sending notifications every 30 seconds to my device. Lets say i am in ViewController B, but the data that is received by the notification is ment to be displayed/used in ViewController A.
Lets say i received two notifications while i was in ViewController B. Then i navigate to ViewController A. How would i get it to display the most recent data that was received by the notification?
You should receive the notification in a (global) 3rd object that will store them, then when the VC A is displayed you'll easily retrive them from that object...
Follow the "shared instance" path used by many iOS classes (even if someone don't like it 'cause they read singletons are evil, I think this's the perfect case to use it).
You can solve it this way:
Create at startup your singleton class that will receive the notifications and keep them in a queue.
Add to the singleton methods to read/consume the notification queue.
From any class you need the data (i.e. your view controller) get the infos you need via the methods above.
This solution keep data manager (notification handling) and presentation (the view controller) separated, I don't see any real cons...
Again, I know singletons have a bad reputation (and often people abuse of this pattern) but you know Apple's NSNotificationCenter have a +defaultCenter class method that return the shared instance (another word for singleton) so I'm quite sure this's the case to use it.
here http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/ you can find a good example how to implement the +sharedInstance (or +defaultCenter or whatever you want to call it) method.
Hope this help.

Communicating view change

Is it best practice to communicate an event or something similar (like successful login) through NSNotificationCenter or is there any other controlling mechanism you can recommend?
As for me I do not like to use NSNotificationCenter because it is overloaded with a big amount of system and custom events.
If you add a lot of observers to NSNotificationCenter, you should not forget to remove these observers, also sometimes it is difficult to know the sequence, in which observing methods will be called. Also NSNotificationCenter doesn't check or manage adding the same observer more than one time(it sometimes becomes a real trouble, when you addObserver not in the correct place).
So: Why just not create some LoginManager singleton which will contain all the needed data and manage all login behaviour? It will contain some data as : isAuthorithed, etc.. And of course if you need to implement Observer pattern, your singleton class can implement this in the same way as NSNotificationCenter
That depends on what the event is and what classes are likely to need to know about it. For login / logout notifications are a good option because many different classes may want to respond tithe event. That doesn't mean that you can't also have a delegate / block callback for use by the class which triggered the login.
Generally, notifications for general things that could be interesting to many classes and direct callbacks for specific events (and the instance which triggered the event).

Resources