When to authenticate to server in iOS app - ios

I have an iOS app that authenticates to a remote API. The server gives back a token that is used for all the next requests. The authentication call is a simple POST to /api/auth.
My question is: where should I make this call in the app ?
I don't know if I should use it in the AppDelegete (willEnterForeground or didBecomeActive), because it may slow down the app launch. Moreover, this is asynchronous and if I try to make other requests in some controllers while the token hasn't returned, there will be errors.
So I thought about doing it in the root controller, but in the case the app was in the background for a long time and comes to foreground in another controller it doesn't work...
The last option would be to watch errors on every call, and re-authenticate when the server responds with a 'token expired' error. In that case I should probably have a special class for HTTP requests and error handling ?
I don't know what option is the best...

Related

QuickFIX/J - how to handle End Of Stream

I am new to QuickFIX/J.
Creating initiator using websocket(frontend-angular, backend-Springboot websocket). Using w.3.
I would like to handle session expired issue. When the FIX server session is expired, it is sending Logout with reason session deactivated. That works fine for already connected connection/session.
Now, after this, trying to initiate connection, it keeps calling back logout and toAdmin with repeating event/error on screen log (as event) "Disconnecting, End Of Stream encountered".
I would like to handle this scenario and want to capture this in my code so that proper message to UI will be sent.
I don't know which QuickFIX/J version you are using, but on versions up to 2.2.0 you could implement the quickfix.SessionStateListener in your application and utilize its onDisconnect() callback.
If you are even using version 2.3.0 you could also use onConnectException() which will handle some additional scenarios that the first callback does not cover.

Twilio Ougoing Voice Call : Token does not allow outgoing calls on first attempt

I have recently started experiencing peculiar behaviour trying to initiate an outgoing phone call Browser -> Phone.
Every time I refresh the browser, my initial call to Twilio.Device.connect(phonecallParams) results in an error:
Received an error from the gateway: {code: 31002, connection: Connection, message: "Token does not allow outgoing calls."}.
If I make a second call to Twilio.Device.connect(phonecallParams), it works.
Also every subsequent call works. But if I refresh the browser then the first call fails again.
It used to work first time, every time. But I last tested this weeks ago.
Now it fails first time, every time.
With the first, failed call:
I get call my back-end to get a token
I return the token to the JS method
I call Twilio.Device.connect(phonecallParams)
I get the error message.
Nothing else happens. There is no attempt to call my TwiML handler.
With the second, successful call:
I get call my back-end to get a token
I return the token to the JS method
I call Twilio.Device.connect(phonecallParams)
My backend method that handles TwiML is called
Call is initiated.
In both cases:
the token construction is identical the same method which creates the token the same way (and includes new OutgoingClientScope(_twilioAccount.TwiMLApplicationSid))
the phonecallParams are identical
Would anyone have a clue as to what could be going on??
Ok, so this turned out to be one of those silly things...
I was calling Twilio.Device.connect() immediately / too soon after requesting my token - the same trigger that requested the token also triggered Twilio.Device.connect().
I now request my outgoing phone token when my page loads, and by the time the user wants to initiate the call the token has been completely set up, wherever it needs to be.

Voip Push: Under what circumstances does didInvalidatePushTokenForType get called?

The documentation for didInvalidatePushTokenForType says its optional to implement and also says this
This method is invoked if a previously provided push token is no
longer valid for use. No action is necessary to request registration.
This feedback can be used to update an app's server to no longer send
push notifications of the specified type to this device.
Why on earth therefore would somebody not want to implement this? If the token is no longer valid then a server will never be able to send Voip pushes to that device again, so doesn't the app on the handset want to know as soon as possible if its invalided so it can send a new token to the server?
I've been trying to search for info and use of didInvalidatePushTokenForType() but it seems everybody just copy and pastes this method into their source code because everybody else has copy and pasted it. But nobody seems to ever do anything with it.
But seems to like it should be a vitally important method to make use of, so why does nobody apparently?
When would the token become invalid?
Thanks for the asking great question.
1) When would the token become invalid?
If we are update app from the App Store then APNS token doesn't change.
reinstalling the OS or Update OS or Reset iOS device then APNS token does change(Upgrade or downgrade OS).
Device token Invalidated or expired after 2 Years.
iOS9 and later device token changes if I reinstall an app.(As per my experience and Knowledge).
Download app from App Store then run your code using X-Code in this case device token will change.
2) Important of didInvalidatePushTokenForType() or Why? didInvalidatePushTokenForType() is optional
Let's clarify about didInvalidatePushTokenForType() method.
Once token got changed then called didInvalidatePushTokenForType() and didUpdatePushCredentials() method, So all code is placed in didUpdatePushCredentials() instead of didInvalidatePushTokenForType().
That's why Developer doesn't give important to didUpdatePushCredentials() method.
Find Reference from Here

Token expiration in Twilio

I'm working on embedding a soft phone into a web page that will go into Odoo (web based ERP system). It will allow inbound and outbound calls for employees.
The token expires every hour. So this means the user will have to refresh the page every hour. I could do an http refresh but if the user is on a call when it does the refresh it will knock them off the call.
How do we get around this so we can build a fully working dialer?
Twilio evangelist here.
I'd suggest using JavaScript to do an asynchronous HTTP request to get a new token from your server and then updating the instance of client with it.
Hope that helps.
Another Twilio evangelist here!
You can actually listen for the offline event on the Twilio.Device object. From the documentation:
.offline( handler(device) )
Register a handler function to be called when the offline event is
fired. This is triggered when the connection to Twilio drops or the
device's capability token is invalid/expired. In either of these
scenarios, the device cannot receive incoming connections or make
outgoing connections. If the token expires during an active connection
the offline event handler will be called, but the connection will not
be terminated. In this situation you will have to call
Twilio.Device.setup() with a valid token before attempting or
receiving the next connection.
So you want something like:
Twilio.Device.offline(function(device) {
fetchTokenFromServer(function(token) {
device.setup(token);
});
});
where fetchTokenFromServer makes the HTTP request that Devin suggested in his answer.
Let me know if this helps.
I just ran into this issue so hopefully my solution can help you and others.
I was using twilio.js v1.3 and tried implementing my offline callback like #philnash recommended, but kept getting the error device.setup is not a function. I then tried using Twilio.Device.setup(newToken) and was able to get the capability token refreshed but also ended up getting a new error: Cannot read property 'setToken' of undefined.
I ended up having to use twilio.js v1.4 to make the error go away. My working solution looks like this:
Twilio.Device.offline(function(device) {
$.ajax(urlToGetNewToken, type: 'get').done(function(newToken) {
Twilio.Device.setup(newToken)
})
})

Can I get response for POST method (Restful)?

I want to make some request from iPhone app to my web service (Rails) and when is data procesed to get response with some string for example.
I read a lot about Apple's push notifications, but I need answer immediately and push notification can late.
I made POST request to my Rails app from my iPhone app, and it works.
First question(I got answer): But, can I get some string as response from Web service (Rails app)?
Second question: When I got request from iPhone app, how to notifies some iPad device for example (with running app) quickly from my Rails web app? One solution is push notification, is there something faster and safer?
If you just want a text string you could do render :text => 'Some string'. If you want/need a more structured response then you should respond with JSON or XML.

Resources