iOS 和 H5 頁面交互(WKWebview 和 UIWebview cookie 設置)

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的時候也是須要分兩種狀況傳遞:ios

  • 1.HTTPRequest 請求URL的時候攜帶 如後端PHP獲取 cookie
  • 2.注入js 目的是讓前端從頁面裏邊獲取到cookie 能夠經過在document.cookie 設置 經過WKWebview 初始化時候把js傳遞過去web

    `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];
}

2.WKWebview 有跨域問題

* 最後要說的是以上方法若是 考慮跨域問題的話,UIWebView 是不會出現的,可是WKWebview是不容許跨域的,這個也是蘋果考慮到安全性的方面,可是是能夠處理的,目前咱們的方案是如下兩種

1.前端經過獲取到cookie後 從新種植一下cookie ,經過 .xxx.com 模糊種植

2.讓後端來處理,能夠把用戶相關信息如 uid傳遞給前端。

相關文章
相關標籤/搜索