IOS UIWebView automatic login verification via cookies

Source: Internet
Author: User
Tags vars

Some notes:

    1. After logging in Via UIWebView, the cookies that are automatically set up by the Web server include the Seesionid in the server.
    2. Cookies are not automatically saved in the app and need to be set up to be available the next time the app is launched.
    3. Automatic login, you need to set the header to bring cookies to the Web server.
    4. Automatic login, requires cookie authentication on the Web server side to log in.

Implementation process:

1. After you have done the sign in page, submit the form to the Web server via get or post, and you can traverse the resulting cookie through the following code. [OBJC]View PlainCopy 
    1. Nshttpcookiestorage *mycookie = [Nshttpcookiestorage sharedhttpcookiestorage];
    2. For (nshttpcookie *cookie in [MyCookie cookies]) {
    3. NSLog (@ "%@", cookie);
    4. }

2. In order to achieve automatic login, the cookie needs to be saved so that it can log on automatically, just add a line of code. [OBJC]View PlainCopy 
  1. Nshttpcookiestorage *mycookie = [Nshttpcookiestorage sharedhttpcookiestorage];
  2. For (nshttpcookie *cookie in [MyCookie cookies]) {
  3. NSLog (@ "%@", cookie);
  4. [[Nshttpcookiestorage Sharedhttpcookiestorage] Setcookie:cookie]; //Save
  5. }

3. When logging in automatically, the last saved cookie needs to be taken out to set the header to the Web server with the following code.

[OBJC]View PlainCopy 
  1. Look for the URL to host the relevant cookie, do not worry, step 2 has automatically set a cookie for the relevant URL information
  2. Nsarray *cookies = [[Nshttpcookiestorage sharedhttpcookiestorage] cookiesforurl:[nsurl URLWithString: HOST]]; //The host here is the domain address of your Web server
  3. For example, your previous login site address is (of course, if you want to add HTTP//, if your server needs port number can also add the upper slogan), then the host here is
  4. Set the header by traversing the cookies to set a header for one
  5. For (Nshttpcookie *cookie in cookies) {
  6. Cookieswithresponseheaderfields method, you need to set a cookie for the URL of the Nsdictionary type header, note that nsdictionary inside the forkey need to be @ " Set-cookie "
  7. Nsarray *headeringcookie = [Nshttpcookie cookieswithresponseheaderfields:
  8. [Nsdictionary Dictionarywithobject:
  9. [[NSString alloc] initwithformat:@ "%@=%@", [Cookie Name],[cookie value]]
  10. Forkey:@ "Set-cookie"]
  11. Forurl:[nsurl Urlwithstring:host]];
  12. With the Setcookies method, the settings are completed so that when a Web page with a URL of host is visited, the set header is automatically attached
  13. [[Nshttpcookiestorage sharedhttpcookiestorage] setcookies:headeringcookie
  14. forurl:[nsurl Urlwithstring:host]
  15. Maindocumenturl: nil];
  16. }

4. The Web server validates the cookie information that comes with the app to complete the login.
If the page you are visiting does not have a cookie-validated code, then go to a page that has a validation cookie such as *loginaction, or tell your colleague to verify the login by adding a cookie to the page you are visiting.

BB: Reprint Please specify source:

IOS UIWebView automatic login verification via cookies

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.