In which view controller delegate method do i request the api.? - ios

I have been developing a weather application in iOS 8 . I need to display the parsed JSON when the view displays. I am not sure in which delegate methods do i need to request the api. i know it should be requested in the background thread. which are the delegate methods run on background thread?

you can call your api in every where!
for example call in : AppDelegate or in initialize viewController.
but remember : your request is async so if the request going to fail or getting timed out you should handle it or notify toy first View Controller.

Related

Is there a way to stop iOS today extension sending API requests in background?

I'm trying to stop sending API requests in background, when the today extension is not visible. API requests are pretty expensive, so I would like to optimize the number of sent requests. Where should I put API request so it will be called only when the today extension become visible and will not be called in background?
I have already tried to set NCUpdateResultNoData however viewDidLoad is called in background in that case. In viewDidLoad I send API request to update the today extension when it becomes visible.
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
completionHandler(NCUpdateResultNoData);
}
Well this is a tricky question, what you can try is to fire your request in viewWillAppear: but this means that your UI will probably not be ready in viewDidAppear: but you can handle this using an activity indicator.
From the docs:
// If the widget has local state that can be loaded quickly, it should
do so before returning from ‘viewWillAppear:’. // Otherwise, the
widget should ensure that the state and layout of its view when
returning from 'viewWillAppear:’ will match that of the last time it
returned from 'viewWillDisappear:', transitioning smoothly to the new
data later when it arrives.”

NSURLProtocol not responding to UIWebviews

I am having an issue with UIWebviews and a couple of custom NSURLProtocols that I have in my app.
All my non web view request are invoked with NSURLSession, so in order for those request to go through the protocols I need to set the setProtocolClasses array on my sessionconfig, at this point everything works as expected.
For my web views, I do a registration on the didFinishLaunchingWithOptions: method in the AppDelegate with the [NSURLProtocol register class[MyProtocol Class]]. If i don't re-register before the execution of the web view request, that web view request won't go through the protocol.
Do you guys have any idea why I have to re-register to my custom protocol every time I try to load a web view request?
What is the request URL? Is it possible that there's another protocol class that gets registered after you? Does canInitWithRequest get called on your class at webview request time?

Watchkit : Handle multiple session sendMessageData requests

I have a WatchOS2 app which displays data on the watch after calling NSURLSession. Since response takes some time, if the user opens another interface controller another call goes to
- (void)session:(WCSession *)session didReceiveMessageData:(NSData *)messageData replyHandler:(void(^)(NSData *replyMessageData))replyHandler
But if previous api output comes then it returns data through reply. Again the second data output should also be send. So this is giving a crash and my app hangs.
Is there a way to stop the previous reply from being sent by closing the request?
No, there is no way to cancel the previous request. It sounds like you are making the "currently visible interface controller" be the delegate of the WCSession, which would be mixing a lot of responsibilities. Instead I'd suggest adding something like a singleton class that is the permanent delegate of WCSession; and it persists and notifies, or dispatches incoming data to the right place.

Swift logout process

I am building an app in swift which interacts with an API. There a two ways a user can be logged out, by clicking a logout button or by receiving a 401 response when calling the API.
I plan on using NSNotificationCenter in my API class to broadcast an event when an unsuccessfully response is received so generic handling of things like 401, 400, 500 can be handled in one place outside of the API class.
To prevent duplicating the logic involved with logging out in multiple places I have created a Class which will clear any existing tokens and present the login view controller. This class can then be used when the logout button is clicked in the view controller or when a 401 response is picked up by the response observer.
My problem is, since the logic is not inside a view controller I am unsure how I can present the login view controller as I do not have access to the method self.presentViewController
You're going to need to pass that responsibility to a view controller.
How I've handled this in the past is using a key-value observer (or RAC) to monitor the active token. When that token goes away, I swap out the root controller.
Where you do this depends on how you've structured things. The app delegate is a reasonable spot.

IOS when to make server call at app launch time

There are a few ways your app can launch:
User opens app
Push notification leads to opening of app.
What is the recommended design to know when to make a async network call to your server to fetch some data? For example, didFinishLaunchingWithOptions gets called at app launcht ime, so would it make sense to put that call in that function? What about putting the call in viewDidLoad of my root view controller? viewDidLoad only gets called once for load while viewWillAppear gets called too frequently.
What's a good pattern or design to handle both cases of app launch.

Resources