美文网首页
WKWebView & UIWebView

WKWebView & UIWebView

作者: L柠_檬 | 来源:发表于2016-12-02 14:44 被阅读168次

    1.内存消耗

    屏幕快照 2016-12-02 上午10.09.41副本 2.png

    2.获取URL、scheme、resourceSpecifier

    - (BOOL)webView:(UIWebView *)webView 
    shouldStartLoadWithRequest:(NSURLRequest *)request 
    navigationType:(UIWebViewNavigationType)navigationType {
    
      NSURL *url = request.URL;
      
      if ([[IPLLoginManager sharedInstance] isScheme:url.scheme])
        {
            [IPLLoginManager sharedInstance].resourceSpecifier = 
            url.resourceSpecifier;
    
            [[IPLLoginManager sharedInstance] getLoadString];
            
            [[IPLLoginManager sharedInstance] getToken];
            
            return NO;
        }
        return YES;
    }
    
    - (void)webView:(WKWebView *)webView 
    decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction 
    decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
        
        NSURL *url = navigationAction.request.URL;
        
        if ([[IPLLoginManager sharedInstance] isScheme:url.scheme])
        {
            [IPLLoginManager sharedInstance].resourceSpecifier = 
            url.resourceSpecifier;
    
            [[IPLLoginManager sharedInstance] getLoadString];
            
            [[IPLLoginManager sharedInstance] getToken];
            
            decisionHandler(WKNavigationActionPolicyCancel);
        }
        else
        {
            decisionHandler(WKNavigationActionPolicyAllow);
    
        }
    
    }
    

    3.获取标题

    - (void)webViewDidFinishLoad:(UIWebView *)webView{
    
        self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
    }
    
    
    - (void)webView:(WKWebView *)webView 
    didFinishNavigation:(WKNavigation *)navigation{
    
         NSLog(@"%@",self.mainWebView.title);
    
    }
    
    

    4.获取ua,通过ua区别PC端还是移动端请求服务器。

    NSString *userAgent = [webView 
    stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    
    NSLog(@"old agent :%@", userAgent);
    
    
     [self.mainWebView evaluateJavaScript:@"navigator.userAgent" 
    completionHandler:^(id result, NSError *error) {
         
         NSLog(@"%@",result);
         
     }];
    
    实际上,只要把自定义的字符串加入ua里面即可~
    注:需在请求前设置
    
        NSString *userAgent = @"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/602.1.46 (KHTML, like Gecko) Mobile/13A137";
        
        NSString *executableFile = @"xxxxx";
        
        NSString *version = [[[NSBundle mainBundle] infoDictionary]
                             objectForKey:(NSString *)kCFBundleVersionKey];
        
        NSString *ua = [NSString stringWithFormat:@"%@ %@ %@",
                        executableFile,
                        version,userAgent];
        
        [[NSUserDefaults standardUserDefaults]
         registerDefaults:@{@"UserAgent" : ua, @"User-Agent" : ua}];
        
        [[NSUserDefaults standardUserDefaults]  synchronize];
    
    

    5.清除页面缓存cookie

    NSHTTPCookie *cookie;
    
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    for (cookie in [storage cookies])
    
    {
    
    [storage deleteCookie:cookie];
    
    }
    

    6.添加返回按钮

    1.8 返回添加关闭按钮
    
    当点击进入两次以上网页,想直接返回app,那么就需要一个关闭按钮
    
    - (void)leftBarButtonClick:(id)sender
    {
        if (self.mainwebView.canGoBack)
        {
            [self.mainwebView goBack];
            [self showWebCloseButton];
        }
        else
        {
            [self popViewController];
        }
    }
    

    相关文章

      网友评论

          本文标题:WKWebView & UIWebView

          本文链接:https://www.haomeiwen.com/subject/feffmttx.html