How to manage the session ID and AFNetworking when using afnetworking

Source: Internet
Author: User

How to manage the session ID and AFNetworking when using afnetworking


As the title implies, I am using AFNetworking in an iOS project in which the application talks to a server. when the user signs in, the server responds by sending back a success flag and the response headers contain the session ID.

I am wondering if AFNetworking automatically sends the session ID with every subsequent request or shoshould I take care of this myself in some way?

For your information, I have no control over the back-end in terms of how requests are authenticated. I am only building a client that talks to the server.


Yes, your session ID shocould be sent automatically once you are logged in, as long as the cookie does not expire before the next request is sent (important detail to be sure ).NSURLConnection, Which AFNetworking uses, takes care of the details for this for you.

On the backend AFNetworking is usingNSURLConnectionWhich in turn automatically updatesNSHTTPCookieStorageTo store the session. You can manipulate or delete the cookies as you see fit by messing with the cookie storage.

Like if you wanted to appear to the service as not logged in, you coshould delete the session cookie associated to that domain. some services I have worked with will error if you are already logged in and attempt to login again. additionally there was no way to check login status. quick fix, get the cookies from URL and delete them:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.