Some notes:
- After logging in Via UIWebView, the cookies that are automatically set up by the Web server include the Seesionid in the server.
- Cookies are not automatically saved in the app and need to be set up to be available the next time the app is launched.
- Automatic login, you need to set the header to bring cookies to the Web server.
- 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
- 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.
[OBJC]View PlainCopy
- Nshttpcookiestorage *mycookie = [Nshttpcookiestorage sharedhttpcookiestorage];
- For (nshttpcookie *cookie in [MyCookie cookies]) {
- NSLog (@ "%@", cookie);
- [[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.
[OBJC]View PlainCopy
- 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]]; //The host here is the domain address of your Web server
- For example, your previous login site address is abc.com (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 http://abc.com
- Set the header by traversing the cookies to set a header for one
- For (Nshttpcookie *cookie in cookies) {
- 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 "
- Nsarray *headeringcookie = [Nshttpcookie cookieswithresponseheaderfields:
- [Nsdictionary Dictionarywithobject:
- [[NSString alloc] initwithformat:@ "%@=%@", [Cookie Name],[cookie value]]
- Forkey:@ "Set-cookie"]
- Forurl:[nsurl Urlwithstring:host]];
- 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
- [[Nshttpcookiestorage sharedhttpcookiestorage] setcookies:headeringcookie
- forurl:[nsurl 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
IOS UIWebView automatic login verification via cookies