IOS UIWebView automatic login verification via cookies

Source: Internet
Author: User

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.
Nshttpcookiestorage *mycookie = [Nshttpcookiestorage sharedhttpcookiestorage];            For (Nshttpcookie *cookie in [MyCookie cookies]) {                NSLog (@ "%@", cookie);            }

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.
Nshttpcookiestorage *mycookie = [Nshttpcookiestorage sharedhttpcookiestorage];            For (Nshttpcookie *cookie in [MyCookie cookies]) {                NSLog (@ "%@", cookies);                [[Nshttpcookiestorage Sharedhttpcookiestorage] Setcookie:cookie]; Save            }

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.
Look for the URL to host the relevant cookie, do not worry, step 2 has automatically set a cookie for the relevant URL information nsarray *cookies = [[Nshttpcookiestorage Sharedhttpcookiestorage] Cookiesforurl:[nsurl Urlwithstring:host]; Here the host is your Web server's domain name///For example, you have been logged in the site address is abc.com (of course, before the Add/HTTP, if your server needs port number can also add the upper slogan), then the host is http://abc.com// Set the header by traversing the cookies to one of the settings headerfor (Nshttpcookie *cookie in cookies) {//Cookieswithresponseheaderfields method, Need to set a Cookie for the URL of the Nsdictionary type header, note that nsdictionary inside the forkey need to be @ "Set-cookie" nsarray *headeringcookie = [Nshttpc                                     Ookie cookieswithresponseheaderfields: [nsdictionary dictionarywithobject:                                                                [[NSString Alloc] initwithformat:@ "%@=%@", [cookie Name],[cookie value]]                                                                         forkey:@ "Set-cookie"] Forurl:[nsurl urlwithstring:host]];//through the Setcookies method, complete the setup so that whenever a Web page with a URL of HOST is visited, it is automatically accompanied by a set header [[Nshttpcoo KiestoragE Sharedhttpcookiestorage] Setcookies:headeringcookie Forurl:[ns    URL Urlwithstring:host] Maindocumenturl:nil]; }

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: http://blog.csdn.net/assholeu/article/details/38585243

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.