iOS afnetworking---cookie (session) keeps the login session state

Source: Internet
Author: User

Afnetworking is the most popular iOS network library, built on foundation technology such as Nsurlconnection, nsoperation and so on, to say no more, and today to record a little bit about using it.

----------------------------------------I'm a split line-------------------------------------

Usually the company's project is to use the login to obtain a token (token), when the other information requested by the user, the body of the POST request, add token parameters can be.

[Self.manager POST: interface parameters:@{@ " parameter name ":123ID  _nullable Responseobject) {}failure:^ (nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {        NSLog ( @" %@ " , [error description]);    }];

But the background server is to use the Web cookie to keep the login session state, so-called cookie is to identify the user identity, the session tracking and stored in the user's local terminal data.

So there is no code specifically for the cookie in afnetworking, but the underlying use is nsurlrequest, so we can get the cookie that the server returned when requested

Nsstring*urlstr=[nsstring stringWithFormat:@ " your interface? username=%@&password=%@", Account,password];
Nsarray *cookies = [[Nshttpcookiestorage sharedhttpcookiestorage] Cookiesforurl: [Nsurl URLWithString:urlStr]]; // Get Cookies
 nsstring*jsessionid=@ ""  ;  for  (nshttpcookie*cookie in  Span style= "color: #000000;" > cookies) { if  ([cookie.name isequaltostring:@ " jsessionid  "  ]) {Jsessionid  =cookie.value; }}  // ps: We can also get the session and other information in the cookie /span> 
// Save the resulting cookie        NSData *data = [Nskeyedarchiver archiveddatawithrootobject:cookies];        [[Nsuserdefaults Standarduserdefaults] setobject:data forkey: @" Cookies " ];        [[Nsuserdefaults standarduserdefaults] synchronize];

Well, through the above method, we get the cookie and session.

The point is, when asked again, we can set cookies:

NSString *cookiestr = [NSString stringWithFormat:@ "jsessionid=%@", Jsessionid]; // you can set the session, or you can add additional information [Self.manager.requestSerializer setvalue:cookiestr Forhttpheaderfield:@ "Cookie"]; // set to Headerfield

You can print:

NSLog (@ "%@", self.manager.requestSerializer.HTTPRequestHeaders);

----------------------------------------------------I'm a split line------------------------------------------

Just said so much, actually said is nonsense, because afnetworking is automatically kept cookie, we do not have to deliberately deal with it (get and upload), unless you have some need

Of course you can also set not to keep cookies

[Self.manager.requestSerializer Sethttpshouldhandlecookies:yes]; // The default is Yes

If your project background is to use a cookie to maintain the status of the login session, depending on the project, the completion of the landing, do not do any processing, direct POST request, Afnetworking automatically keep the cookie, do not have to upload a cookie (session).

Of course you can also delete cookies

Nshttpcookiestorage *cookiejar = [Nshttpcookiestorage sharedhttpcookiestorage];       *cookies = [Nsarray arraywitharray:[cookiejar cookies]];        for inch cookies) {          if  ([[[Cookie Name] Isequaltostring:key]) {              [Cookiejar Deletecookie:cookie];}      }  }  

iOS afnetworking---cookie (session) keeps the login session state

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: info-contact@alibabacloud.com 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.