How to manage sessions using AFNetworking?

As the name suggests, I use AFNetworking in an iOS project in which the application is talking to the server. When the user logs in, the server responds by sending back the success flag, and the response headers contain the session identifier.

I am wondering if AFNetworking will automatically send a session identifier with each subsequent request, or should I take care of this somehow?

For your information, I have no control over the internal content in terms of request authentication. I am only building a client that talks to the server.

+52
ios cookies session afnetworking
Jun 11 2018-12-12T00:
source share
2 answers

Yes, your session ID should be sent automatically after logging in, if the cookie does not expire before the next request is sent (an important detail to be sure). NSURLConnection , which uses AFNetworking, takes care of the details for this for you.

The backend network AFNetworking uses NSURLConnection , which in turn automatically updates NSHTTPCookieStorage to store the session. You can manipulate or delete cookies as you wish by tinkering with the cookie storage.

As if you wanted the service not to appear in the log, you can simply delete the session cookie associated with this domain. Some of the services I worked with will be erroneous if you are already logged in and are trying to log in again. In addition, it was not possible to check the login status. Quick fix, extract the cookies from the URL and delete them:

 NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL: networkServerAddress]; for (NSHTTPCookie *cookie in cookies) { [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; } 

From the developer himself

+113
Jun 14 '12 at 19:10
source share

It depends on the specification provided by the service with which you interact. I worked with similar services, and they explicitly stated in their documentation that: In order to keep the session valid, I have to poll the service every few seconds by sending int "1".

However, if possible, please post the service name or any link that we can read. If this is any private company API, they should describe the use of the session identifier that they return.




Basic technologies will take care of this, however, if you want to save these cookies, then this answer is for another question.

Saving cookies in iOS app

+4
Jun 14 2018-12-12T00:
source share



All Articles