Tracking when a UIViewcontroller is instantiated or appear - ios

I'm looking for a solution to track everything a UIViewController gets instantiated or is displayed on screen without using a base view controller as a superclass.
I know Firebase Analytics do it for the event screen_view, but haven't found a similar way to do it yet.
Any ideas on how to achieve this?

Related

Current location outside of UIViewController

Is there way I can get current location without using UIViewController?
I'm making a SDK and I don't want to make UIViewController in it. I want to make only modal classes. I'm looking a way to avoid UIViewControllers completely for this SDK.
Thanks in Advance.

Communication from SKScene to UIViewController

I'm using Google AdMob and to present ads, I do so from the ViewController. Right now, I have a button in my GameScene that is supposed to tell the ViewController to display the ads.
At first, I tried delegation, but that didn't work since SKScene already has a delegate, but then I stumbled upon this answer that showed a way for the SKScene to call a method in its ViewController. The answers seems sketchy, though, as the SKScene holds a reference to its ViewController. My understanding, as well as according to this, was that this is bad practice, assuming that in my case, the SKScene is the View in the MVC structure.
So how should I notify the ViewController of events occurring in the SKScene?
You can check my answer which describes how to communicate SKScene with UIViewController using delegate pattern.

Present View Controller without being on a View Controller - Swift / Objective-C

I'm creating an app that implements a Facebook and a Twitter service. In my view I have a button that toggles sharing on Facebook/Twitter or not. If Facebook/Twitter aren't connected, then the button will show "connect to".
When I click the button, a method in my controller (not my viewcontroller) will try to toggle the value because this controller tracks the state of my app. When I'm not connected to a social network my controller will notice and will call the correct service. This service will start a webview to provide the user credentials.
And now my question:
When I'm in my service and I need to provide credentials via a webview. When I want to show this webview I need to pass a View Controller that will handle the presenting. How do I provide a viewcontroller here?
Approaches I found:
Call the appdelegate singleton and get the current presenting
viewcontroller (I find this dirty but correct me if I'm wrong).
Since I'm injecting my service into my controller in
appdelegate.didFinishLaunchingWithOptions I could inject the UIWindow
of the appdelegate and ask for the current presenting viewcontroller
(Is almost the same as the first approach)
Create a protocol implemented by a viewcontroller. The service has a property that is equal to that protocol and in my app delegate inject the
viewcontroller into the service.
Have a property in your controller
that will be the presentingviewcontroller and let your controller
implement the approach #3 protocol. When a function of that protocol
is fired, the controller will handle it and use the
presentingviewcontroller property for the webview. I only need to
inject a viewcontroller into my controller in the appdelegate.
Don't implement anything view related in that service.
My current implementation is approach #3.
If some things are not clear, please let me know.
Any help is appreciated!
Thanks in advance!
I was hoping this question would of got more attention, I was interested to know how other people would handle this situation.
As you have already stated, there are a few ways to achieve what you need, but the answer depends on having knowledge of the application.
I would definitely try to keep business logic and UI as separate as possible, there are two methods that I can think of to do this, but they are pretty much the same thing.
Make the (non UI) controller responsible for the login check with a callback function, you can then leave the controller responsible for business logic and the calling ViewController responsible for rendering any UI as a result of that check.
As you suggested, using protocols, your controller could have a delegate that will be a ViewController that conforms to your protocol. Then usage would just be getting your shared instance, setting the delegate and calling the required fuctionality.
I would likely favor option 2 to be more Swift-like as it is a protocol orientated language.
If the controller is acting globally and accessed from anywhere within the application you could potentially end up duplicating a lot of code to make these checks, you could get around this by creating an extension of UIViewController which provides the functionality to interact with the controller.
I hope this helps, Would definitely be interesting to see the way other people would approach this.

How to fetch data of a web service each time a View Controller is shown?

I am struggling a little bit with the lifecycle of a UIViewController.
Basically, I have an application that consists of a UINavigationController, that has currently an UITableViewController as the root view controller.
The UITableView of this UITableViewController is updated with an asynchronious call to a web service.
Up to that point, everything works fine!
In general, I would like the UITableViewController to reload the data for the TableView every time the ViewController is shown.
That means, if I dismiss the app and start another app and then return to my application the data should be always reloaded from the web service. Basically, like the Twitter app as an example that updates the tweets each time when the app is restarted.
To achieve this, I placed the call to the web service in different places to test the behavior.
I tried, viewDidLoad, viewWillAppear & viewDidAppear but all of these methods are just called once when the viewController is created.
I did some research here in stackoverflow and there was the hint to use NSNotification applicationDidBecomeActive which does work but I am not sure if this is the proper solution if I add more ViewControllers to my app in future because I want the update of the viewController only when this controller is also shown/visible to the user.
Any suggestions on this issue are highly welcome!
Thanks a lot
Neo74
There is nothing wrong using applicationDidBecomeActive to update the information from the web services, and if you are planning to add more view controllers in the future, it is even better to have to web services logic outside of a single view controller.
What you shouldn't do is put the code to access your WS inside the application delegate, instead create a singleton class responsible of this.

iOS: UIViewController is Not Being Pushed Problem

I'm working on a new app where there is a "main" UIViewController with some UIButtons and once the user clicks on the buttons, the application is navigating to another UIViewController.
For some reason, the UIViewControllers are not opened, when you click on the button nothing happens. Several points that anyone who tries to help should be aware of
I'm using code and interface that are very similar to another app that I had created and is working fine (I triple checked the code, and it's the same in the area that makes the UIViewControllers navigation)
I triple checked the Interface Builder items and their connection to the IBOutlet us fine (view is connected to view, File's Owner is set correctly, etc...)
I inserted NSLogs to the UIViewController that is not being pushed and I can see that initWithNibName is being called, but viewDidLoad is not being called.
I'm using basic-simple code to create and push the UIViewController: alloc+initWithNibName and then presentModalViewController or pushViewController (none of them work) and as I said, in another app with the same mechanism it works fine.
I don't know where to look! Maybe I unchecked by mistake some "Enable" button in Interface Builder or something like that.
Did anyone encounter something like that or may have some new thought regarding to where I should be looking?
Hmm, strange. Things i can come up with now are: dit you call [super ] in your viewDidLoad?
Is it stacked behind another view? Run instruments to see if all the things on the pushed view are allocated, so you know if the whole view is loaded. And you use a navigation controller? than you use pushViewController. When you are not and want a model you use presentmodalviewcontroller.

Resources