iOS

iOS WKWebView 免登陆

2017-03-02  本文已影响869人  皓皓大帝

简介:

就不复述官方对WKWebView的描述了,随便搜一下就有一大推。简单的说,WKWebView是iOS8之后推出的,与UIWebView作用相同,但是在加载速度、内存占用等方面都有显著提升。

常用的协议方法:

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation { 
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
}
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
}

上面这几个方法都不是我在做免登陆时用到的主要方法,哈哈哈!

进入正题,网页中的免登陆(有点瑕疵)

需求:加载某个网页(例:简书),第一次加载需要登陆,退出网页后下次加载时避免二次登陆。

- (WKWebView *)webView {
    if (!_webView) {
        WKUserContentController* userContentController = [[WKUserContentController alloc] init];
        WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
        config.userContentController = userContentController;
        WKProcessPool *processPool = [[WKProcessPool alloc] init];
        config.processPool = processPool;
        
        WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource:[self cookieJavaScriptString] injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        
        [userContentController addUserScript:cookieScript];
        
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
        
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
    }
    return _webView;
}
//配置config,获取cookieString
- (NSString *)cookieJavaScriptString {
    NSMutableString *cookieString = [[NSMutableString alloc] init];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *cookieDic = [userDefault objectForKey:self.urlHost];
    for (NSString *key in cookieDic) {
        NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:[cookieDic objectForKey:key]];
        NSString *excuteJSString = [NSString stringWithFormat:@"document.cookie='%@=%@';", cookie.name, cookie.value];
        [cookieString appendString:excuteJSString];
    }
    //执行js
    return cookieString;
}
//发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    self.urlHost = navigationAction.request.URL.host;
    //判断是否存host
    if (self.urlHost) {
        if (!navigationAction.request.allHTTPHeaderFields[@"Cookie"]) {
            decisionHandler(WKNavigationActionPolicyCancel);
            [self loadRequestWithString:navigationAction.request.URL];
        }else {
            decisionHandler(WKNavigationActionPolicyAllow);
        }
    }else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

//手动添加cookie
- (void)loadRequestWithString:(NSURL *)url {    
    NSMutableString *cookieValue = [NSMutableString string];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *cookieDic = nil;
    if (self.urlHost) {
        cookieDic = [userDefault objectForKey:self.urlHost];
    }
    
    for (NSString *key in cookieDic) {
        NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:[cookieDic objectForKey:key]];
        NSString *appendString = [NSString stringWithFormat:@"%@=%@;", cookie.name, cookie.value];
        [cookieValue appendString:appendString];
    }
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:cookieValue forHTTPHeaderField:@"Cookie"];
    [self.webView loadRequest:request];
}
//接收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
    
    //将获取的cookie本地化
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *cookieDic = [[NSMutableDictionary alloc] initWithDictionary:[userDefaults objectForKey:self.urlHost]];
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
    NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
    for (NSHTTPCookie *cookie in cookies) {
        [cookieDic setObject:[cookie properties] forKey:cookie.name];
    }
    
    [userDefaults setObject:cookieDic forKey:self.urlHost];
    
    decisionHandler(WKNavigationActionPolicyAllow);
    
}
上一篇下一篇

猜你喜欢

热点阅读