IOS UIWebView automatic login verification via cookies
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Description</strong></p></p> <ol> <ol> <li>After logging in Via uiwebview, the cookies that are automatically set up by the Web server include the Seesionid in the Server.</li> <li>Cookies are not automatically saved in the app and need to be set up to be available the next time the app is Launched.</li> <li>Automatic login, you need to set the header to bring cookies to the Web Server.</li> <li>Automatic login, requires cookie authentication on the Web server side to log in.</li> </ol> </ol><p><p></p></p><p><p><strong>Implementation Process:</strong></p></p>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.<pre class="prettyprint perl"><pre class="prettyprint perl"><span class="variable">*mycookie = [nshttpcookiestorage sharedhttpcookiestorage];<br><span class="keyword"><span class="variable">*cookie in [mycookie cookies]) { NSLog (<span class="variable">@ "<span class="variable">%@<span class="string">", cookies);}</span> </span> </span></span></span></span></pre></pre>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.<pre class="prettyprint perl"><pre class="prettyprint perl"><span class="variable">*mycookie = [nshttpcookiestorage sharedhttpcookiestorage]; <span class="keyword"> <span class="variable">*cookie in [mycookie cookies]) { NSLog (<span class="variable">@ "<span class="variable">%@<span class="string">", cookies); [[nshttpcookiestorage sharedhttpcookiestorage] setcookie:cookie]; save}</span> </span> </span> </span></span></span></pre></pre><p><p>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.</p></p><pre class="prettyprint cs"><span class="comment">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];<span class="comment">The host here is the domain address of your web Server.<span class="comment"><span class="comment">//such as 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 is Http://abc.com<span class="comment">//set The header by traversing the cookie to one of the settings Header<span class="keyword">for (nshttpcookie *cookie <span class=" Keyword ">in cookies" {<span class="comment">//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 = [ Nshttpcookie cookieswithresponseheaderfields: [nsdictionary dictionarywithobject: [[NSString alloc] initWithFormat:< Span class= "string" >@ "%@=%@", [cookie name],[cookie <span class="keyword">value]] forkey:<span class="string">@ " Set-cookie "] Forurl:[nsurl urlwithstring:host]; <span class="comment">//through The Setcookies method to complete the setup so that whenever a Web page with a URL of host is visited, it is automatically accompanied by a set header [[nshttpcookiestorage sharedhttpcookiestorage] Setcookies:headeringcookie Forurl:[nsurl urlwithstring:host] mainDocumentURL:nil]; }</span></span></span></span></span></span></span></span></span></span></span></pre><p><p>4. The Web server validates the cookie information that comes with the app to complete the Login.</p></p><p><p>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.</p></p><p><p>IOS UIWebView automatic login verification via cookies</p></p></span>
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