iOS 和 H5 頁面交互(WKWebview 和 UIWebview cookie 設置) 主要記錄關於cookie相關的坑 1. UIWebview 1. UIWebview 相對比較簡單 直接通過 NSHTTPCookieStorage 設置cookie就能實現。 代碼部分 2. 如果在第一次 ...
iOS 和 H5 頁面交互(WKWebview 和 UIWebview cookie 設置)
主要記錄關於cookie相關的坑
1. UIWebview
1. UIWebview 相對比較簡單 直接通過 NSHTTPCookieStorage 設置cookie就能實現。
代碼部分
```
NSURL *cookieHost = [NSURL URLWithString:self.domain];
// 設定 cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[cookieHost host], NSHTTPCookieDomain,
[cookieHost path], NSHTTPCookiePath,
self.cookieKey, NSHTTPCookieName,
self.cookieValue, NSHTTPCookieValue,
nil]];
// 加入cookie
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
```
2. 如果在第一次請求的時候需要在HTTPRequest 通過setValueForKey設置 HeaderValue
2. WKWebview
在使用WKWebview的時候也是需要分兩種情況傳遞:
- 1.HTTPRequest 請求URL的時候攜帶 如後端PHP獲取 cookie
2.註入js 目的是讓前端從頁面裡邊獲取到cookie 可以通過在document.cookie 設置 通過WKWebview 初始化時候把js傳遞過去
`
WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieValue injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
3.NSHTTPCookieStorage 似乎不攜帶沒問題,因為我們目前沒有通過這個傳遞cookie
網上參考別人的方法是要實現下麵幾個步驟,但是我們項目並沒有按照這三種必要方式,但是可以做個參考:
WKWebview三個處理步驟: (1)iOS11,WKHTTPCookieStore 直接傳遞。(如果是只支持iOS11,下麵兩步可以不做); (2)iOS8-iOS10, js註入; (3)PHP攜帶cookie方式
相關代碼
#pragma mark - WKWebview
// iOS11
- (void)setWkCookie:(WKWebView *)wkWebview completionHandler:(nullable void (^)(void))comple {
NSURL *cookieHost = [NSURL URLWithString:self.domain];
// 設定 cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[cookieHost host], NSHTTPCookieDomain,
[cookieHost path], NSHTTPCookiePath,
self.cookieKey, NSHTTPCookieName,
self.cookieValue, NSHTTPCookieValue,
// [NSDate dateWithTimeIntervalSinceNow:30*60*60],NSHTTPCookieExpires,
nil]];
// 加入cookie
//發送請求前插入cookie;
if (@available(iOS 11.0, *)) {
WKHTTPCookieStore *cookieStore = wkWebview.configuration.websiteDataStore.httpCookieStore;
[cookieStore setCookie:cookie completionHandler:^{
comple?comple():nil;
}];
} else {
}
}
// JS攜帶cookie的形式
- (void)setWkJsCookie:(WKUserContentController *)userContentController {
// 單個cookie,多個的話,再加上document.cookie ='%@=%@';一次
NSString *cookieStr = [NSString stringWithFormat:@"document.cookie ='%@=%@';",self.cookieKey,self.cookieValue];
WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieStr injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[userContentController addUserScript:cookieScript];
}
// PHP攜帶cookie的形式
- (void)setWkPHPCookie:(NSMutableURLRequest *)request {
//通過host關聯cookie。
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];
}
// cookie重覆,先放到字典進行去重,再進行拼接
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 forHTTPHeaderField:@"Cookie"];
}
#pragma mark - Webview
// 客戶端添加cookie
- (void)setWebCookie {
NSURL *cookieHost = [NSURL URLWithString:self.domain];
// 設定 cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[cookieHost host], NSHTTPCookieDomain,
[cookieHost path], NSHTTPCookiePath,
self.cookieKey, NSHTTPCookieName,
self.cookieValue, NSHTTPCookieValue,
nil]];
// 加入cookie
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}