Ios-wkwebview carry a cookie to send an HTTP request, the cookie expires

Source: Internet
Author: User

Send Request code:

NSString *testurl = @ "Http://10.22.122.7:8081/test2_action/view_index"; Nsurl *url = [nsurl Urlwithstring:testurl]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url CachePolicy: Nsurlrequestuseprotocolcachepolicy timeOutInterval:60]; nsstring *value = [nsstring stringWithFormat:@ "wap_0000e86bebfc4514987ae63acf9d19dd=123",]; [Request Setvalue:value Forhttpheaderfield:@ "Cookie"]; [_webview loadrequest:request];

Results: In-page jump, cookie invalidation

Problem cause Analysis:

Wkwebview carries a cookie on the first load, and then Wkwebview automatically clears the cookie when the page jumps in the Wkwebview, causing the user to log in two times.

Workaround:

When creating a WebView object, place the cookie in the Wkwebview to carry the cookie for all requests.

The following is the implementation code: (Here the cookie does not work when the first request, in the H5 page to jump again when it will work, unknown reason)

wkusercontentcontroller* Usercontentcontroller = [[Wkusercontentcontroller alloc] init]; Wkuserscript * Cookiescript = [[Wkuserscript alloc]initwithsource: [nsstring stringWithFormat:@ "document.cookie = ' wap_cookie_enable=1 ';d ocument.cookie = ' Wap_ 0000e86bebfc4514987ae63acf9d19dd=%@ '; ", user. WAP_0000E86BEBFC4514987AE63ACF9D19DD] Injectiontime:wkuserscriptinjectiontimeatdocumentstart fo Rmainframeonly:NO]; [Usercontentcontroller Adduserscript:cookiescript]; Wkwebviewconfiguration *configuration = [[Wkwebviewconfiguration alloc] Init];configuration.usercontentcontroller = Usercontentcontroller; [Configuration.usercontentcontroller Addscriptmessagehandler: Selfname:@ "Redirect"];  Self.webview = [[[Wkwebview alloc] initWithFrame:self.view.bounds configuration:configuration] autorelease]; 

Note the point:

1, the creation of the WebView when the cookie, the beginning of the request when the time to add a cookie,

2. Ensure that the cookie that starts sending the request is consistent with the cookie here.

Ios-wkwebview carry a cookie to send an HTTP request, the cookie expires

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: 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.