WKWebView之同步Cookie - WKWebView踩坑记录

96
Caiflower
0.4 2016.12.16 00:47* 字数 320

iOS8之后有了WKWebView,相信都知道WKWebView比UIWebView性能好很多,然而真正用起来,你会发现WKWebView各种坑...我曾经有几次想换回UIWebView,网上看了各种关于WKWebView的,发现很多最终都换回了UIWebView...

下面将踩过的坑记录下

  • 关于Cookie同步
    1. WKWebView有自己的缓存机制,如果想同步session需要注意一下几个地方
1.使用全局的processPool,
@interface XXWebKitSupport : NSObject
@property (nonatomic, strong,readonly) WKProcessPool *processPool;
+ (instancetype)sharedSupport;
+ (WKWebView *)createSharableWKWebView;
@end

@interface XXWebKitSupport ()
@property (nonatomic, strong) WKProcessPool *processPool;
@end


@implementation XXWebKitSupport

+ (instancetype)sharedSupport {
    static XXWebKitSupport *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [XXWebKitSupport new];
    });
    return  _instance;
}

- (instancetype)init {
    if (self = [super init]) {
        self.processPool = [WKProcessPool new];
    }
    return self;
}

+ (WKWebView *)createSharableWKWebView {

    WKUserContentController* userContentController = [WKUserContentController new];
    
    NSMutableString *cookies = [NSMutableString string];
    WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource:[cookies copy]
                                                         injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                                      forMainFrameOnly:NO];
    [userContentController addUserScript:cookieScript];
    
    WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
    // 一下两个属性是允许H5视屏自动播放,并且全屏,可忽略
    configuration.allowsInlineMediaPlayback = YES;
    configuration.mediaPlaybackRequiresUserAction = NO;
    // 全局使用同一个processPool
    configuration.processPool = [[XXWebKitSupport sharedSupport] processPool];
    configuration.userContentController = userContentController;
    
    WKWebView *wk_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
    
    return wk_webView;
}

  1. 在WKNavigationDelegate代理方法中将cookie设置到本地
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
   
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
    // 获取cookie,并设置到本地
    NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
        for (NSHTTPCookie *cookie in cookies) {
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }
    decisionHandler(WKNavigationResponsePolicyAllow);
}

3.在WKWebView加载请求的时候注入Cookie

NSURL *url = [NSURL URLWithString:urlString];
NSMutableString *cookies = [NSMutableString string];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
// 一般都只需要同步JSESSIONID,可视不同需求自己做更改
NSString * JSESSIONID;
// 获取本地所有的Cookie
NSArray *tmp = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    for (NSHTTPCookie * cookie in tmp) {
        if ([cookie.name isEqualToString:@"JSESSIONID"]) {
            JSESSIONID = cookie.value;
            break;
        }
    }
 if (JSESSIONID.length) {
      // 格式化Cookie
      [cookies appendFormat:@"JSESSIONID=%@;",JSESSIONID];
  }
// 注入Cookie
[requestObj setValue:cookies forHTTPHeaderField:@"Cookie"];
// 加载请求
[self.wk_webView loadRequest:requestObj];

4.经过以上几步已经可以同步Cookie了,第一次请求是没有jsessionid的,服务器会返回一个给你,然后我们在代理方法中的response中拦截并注入到本地,在下一次加载的时候再从本地取出并注入,从而达到同步Cookie的目的

之前有试过在创建WKWebView的时候使用js脚本注入,但是没有成功,代码如下,

+ (WKWebView *)_createContentSharableWKWebView
{
    WKUserContentController* userContentController = [WKUserContentController new];
    
    NSMutableString *cookies = [NSMutableString string];
    NSDictionary *constantCookies = [[self sharedSupport] constantCookies];
    [constantCookies enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
        [cookies appendFormat:@"document.cookie='%@=%@';\n", key, obj];
    }];
    
    WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource:[cookies copy]
                                                         injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                                      forMainFrameOnly:NO];
    [userContentController addUserScript:cookieScript];

    WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
    configuration.processPool = [[_SFWebKitSupport sharedSupport] processPool];
    configuration.userContentController = userContentController;
    
    WKWebView *wk_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
    
    return wk_webView;
}

具体什么原因导致的,我也不是很清楚,希望知道的朋友告知~,也有可能是因为我项目的需求是同步jsessionid导致的,因为jsessionid是HtppOnly,不允许通过js脚本修改,

此为踩坑第一篇,回头再继续第二篇

日记本
Web note ad 1