Wkwebview free login, configure cookies, free login

Source: Internet
Author: User

Requirements: Pure WebApp First Login Remember the account password, quit the app, the next time you start to free login

The first step:

When you create a Wkwebview object, select Custom configuration to load the webpage with a cookie. If the cookie is stored locally, then the cookie is directly configured to Wkwebview, which can be automatically carried on the page after loading, and no cookies are cached locally, then jump to the login page.

-(Wkwebview *) WebView

{

if (_webview = = nil)

{

Wkusercontentcontroller *usercontrntcontroller = [[Wkusercontentcontroller alloc] init];

Wkwebviewconfiguration *config = [[wkwebviewconfiguration alloc] init];

Config. Usercontentcontroller = Usercontrntcontroller;

Wkprocesspool *processpool = [[wkprocesspool alloc] init];

Config. processpool = Processpool;

wkuserscript *cookiescript = [[wkuserscript alloc] initwithsource: [ Self Cookiejavascriptstring] injectiontime: Wkuserscriptinjectiontimeatdocumentstart forMainFrameOnly :YES];

[Usercontrntcontroller adduserscript:cookiescript];

_webview = [[wkwebview Alloc] initwithframe:cgrectmake 00 Mainscreen].bounds.size.widthuiscreen Mainscreen].bounds.height-knavigationbarheight) Configuration:config];

_webview. navigationdelegate = self;

_webview. Uidelegate = self;

_webview. Allowsbackforwardnavigationgestures = YES;

}

return _webview;

}

Step Two:

Get local Cookie,cookie local persistence use Web page Urlhost as the key value, ensure that the same cookie page is required to obtain the corresponding cookie via urlhost, and take out the contents of the cookie locally

Configure config to get cookiestring

-(NSString *) cookiejavascriptstring

{

nsmutablestring *cookiestring = [[nsmutablestring alloc] init];

Nsuserdefaults *userdefault = [nsuserdefaults standarduserdefaults];

nsdictionary *cookiedic = [Userdefault objectforkey: Self. Urlhost];

For (nsstring *key in cookiedic) {

Nshttpcookie *cookie = [[nshttpcookie alloc] initwithproperties:[cookiedic Objectforkey:key]];

nsstring *excutejsstring = [nsstring stringWithFormat:@ "document.cookie= '%@=%@ ';", Cookie. Name,cookie. value];

[Cookiestring appendstring:excutejsstring];

}

return cookiestring;

}

Step Three:

Before the Web page jumps, before sending a request to the server to determine whether to carry a cookie, if not carry a cookie, cancel the jump, manually add a cookie in the jump

Before sending a request, decide whether to jump

-(void) WebView: (wkwebview *) WebView decidepolicyfornavigationaction: (nonnull wknavigationaction *) Navigationaction Decisionhandler: (nonnull Void (^) (wknavigationactionpolicy)) Decisionhandler

{

Self . urlhost = navigationaction. request. URL. host;

if (self. Urlhost) {

if (!navigationaction. Request. allhttpheaderfields[@ "Cookie"]) {

Decisionhandler (Wknavigationactionpolicycancel);

[self loadrequestwithstring:navigationaction. Request. URL];

} Else {

Decisionhandler (wknavigationactionpolicyallow);

}

} Else {

Decisionhandler (Wknavigationactionpolicyallow);

}

}

Adding cookies manually

-(void) loadrequestwithstring: (nsurl *) URL

{

nsmutablestring *cookievalue = [nsmutablestring string];

Nsuserdefaults *userdefault = [nsuserdefaults standarduserdefaults];

nsdictionary *cookiedic = nil;

if (self. Urlhost) {

Cookiedic = [Userdefault objectforkey: Self. Urlhost];

}

For (nsstring *key in cookiedic) {

Nshttpcookie *cookie = [[nshttpcookie alloc] initwithproperties:[cookiedic Objectforkey:key ]];

nsstring *appendstring = [nsstring stringWithFormat:@ "%@=%@", Cookie. Name,cookie. value];

[Cookievalue appendstring:appendstring];

}

Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl: url];

[Request Addvalue:cookievalue Forhttpheaderfield:@ "Cookie"];

[self. WebView loadrequest: request];

}

Fourth Step:

To save the cookie, the first or second cookie will be required to log in when it expires, the cookie on the Web request is saved locally and the next load needs to be used

After receiving the response, decide whether to jump

-(void) WebView: (wkwebview *) WebView decidepolicyfornavigationresponse: (nonnull Wknavigationresponse *) Navigationresponse Decisionhandler: (nonnull Void (^) (Wknavigationresponsepolicy )) Decisionhandler

{

Localization of acquired cookies

Nsuserdefaults *userdefault = [nsuserdefaults standarduserdefaults];

nsmutabledictionary *cookiedic = [[nsmutabledictionary alloc] initwithdictionary: [ Userdefault Objectforkey:selfurlhost]];

nshttpurlresponse *response = (nshttpurlresponse *) navigationresponse. Response;

nsarray *cookies = [nshttpcookie cookieswithresponseheaderfields: [Response Allheaderfields ] Forurl: Response. URL];

For (nshttpcookie *cookie in cookies) {

[Cookiedic Setobject:[cookie Properties] Forkey:cookie. Name];

}

[Userdefault setobject:cookiedic forkey: Self. Urlhost];

Decisionhandler (Wknavigationresponsepolicyallow);

}

Note: My slef.urlhost here is by default the domain name of the request H5

Wkwebview free login, configure cookies, free login

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.