IOS and H5 page interactions (WKWebView and UiWebView cookie settings)

iOS and H5 page interaction (WKWebview and UIWebview cookie settings)

Main Record the cookie-related pits

1. UIWebview

1. UIWebview is relatively simple and can be achieved directly by setting cookies through NSHTTPCookieStorage.

code part

```
NSURL *cookieHost = [NSURL URLWithString:self.domain];
// setting cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[cookieHost host], NSHTTPCookieDomain,
[cookieHost path], NSHTTPCookiePath,
self.cookieKey , NSHTTPCookieName,
self.cookieValue, NSHTTPCookieValue,
nil]];
// Join cookie
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
```

2. WKWebview

When using WKWebview, it needs to be passed in two situations:

  • 1. HTTPRequest carries the following when requesting the URL PHP obtains cookies on the side
  • 2. The purpose of injecting js is to allow the front end to obtain cookies from the inside of the page, which can be set in document.cookie when initialized by WKWebview Pass js over

    `WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieValue injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];

  • 3. NSHTTPCookieStorage does not seem to be a problem, because we currently do not pass cookies through this

The method of referring to others online is to implement the following steps, but our project does not follow these three necessary methods, but you can make a reference:

Three processing steps of WKWebview: (1) iOS11, WKHTTPCookieStore is directly delivered. (If only iOS11 is supported, the following two steps can be omitted); (2) iOS8-iOS10, js injection; (3) PHP carrying cookie method

#pragma mark-WKWebview
// iOS11
- (void)setWkCookie:(WKWebView *)wkWebview completionHandler:(nullable void (^)(void))comple {< br />
NSURL *cookieHost = [NSURL URLWithString:self.domain];
// Set cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:< br /> [cookieHost host], NSHTTPCookieDomain,
[cookieHost path], NSHTTPCookiePath,
self.cookieKey, NSHTTPCookieName,
self.cookieValue, NSHTTPCookieValue,
// [NSDate dateWithTimeIntervalSinceNow :30*60*60],NSHTTPCookieExpires,
nil]];

// add cookie
// insert cook before sending request ie;
if (@available(iOS 11.0, *)) {
WKHTTPCookieStore *cookieStore = wkWebview.configuration.websiteDataStore.httpCookieStore;
[cookieStore setCookie:cookie completionHandler:^{

comple?comple():nil;
}];
} else {


}

}< br />
// The form of JS carrying cookies
- (void)setWkJsCookie:(WKUserContentController *)userContentController {
// single cookie, if multiple, plus document.cookie = '%@=%@';Once
NSString *cookieStr = [NSString stringWithFormat:@"document.cookie ='%@=%@';",self.cookieKey,self.cookieValue];
WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieStr injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[userContentController addUserScript:cookieScript];
}

// PHP carrying cookies Form
- (void)setWkPHPCookie:(NSMutableURLRequest *)request {
//Associate cookie through host.
NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];
NSMutableString *cookieValue = [NSMutableString stringWithFormat:@""];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieStorage cookies]) {
[cookieDic setObject:cookie.value forKey:cookie.name];
}
if ([cookieDic objectForKey:[CookieManager shareInstance].cookieKey]) {
[cookieDic removeObjectForKey:[CookieManager shareInstance].cookieKey];
}

// If the cookie is repeated, first put it in the dictionary for deduplication and then splicing
for (NSString *key in cookieDic) {
NSString *appendString = [NSString stringWithFormat:@"%@=%@;", key, [cookieDic valueForKey:key]];
[cookieValue appendString:appendString ];
}

[cookieValue appendString:[NSString stringWithFormat:@"%@ = %@;",self.cookieKey,self.cookieValue]];
[request addValue :cookieValue forH TTPHeaderField:@"Cookie"];
}
#pragma mark-Webview
// Client add cookie
- (void)setWebCookie {
< br /> NSURL *cookieHost = [NSURL URLWithString:self.domain];
// Set cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[ cookieHost host], NSHTTPCookieDomain,
[cookieHost path], NSHTTPCookiePath,
self.cookieKey, NSHTTPCookieName,
self.cookieValue, NSHTTPCookieValue,
nil]];
/ / Join cookie
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}

2.WKWebview has cross-domain problem

* The last thing to say is that if the above method considers cross-domain issues, UIWebView will not appear, but WKWebview does not allow cross-domain, this One is also Apple’s consideration of security, but it can be handled. At present, our plan is the following two

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3917 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.