WKWebView 使用

import "JspWebViewController.h"

import "LoginViewController.h"

import "AppDelegate.h"

import "IdleWindow.h"

import "UIDevice+TFDevice.h"

import <WebKit/WebKit.h>

@interface JspWebViewController ()<WKScriptMessageHandler,WKUIDelegate,WKNavigationDelegate> //(遵守的协议)
@property (nonatomic, strong) WKWebView *webView;
@property (strong, nonatomic) UIBarButtonItem *backItem0;
@property (strong, nonatomic) UIButton *btn;
@property(nonatomic,strong)UIAlertView *loadingAlert;
//@property (strong,nonatomic) UIAlertController *loadingAlert;

@end

@implementation JspWebViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    [self configWKWebView];

}

  • (void)configWKWebView{

    WKUserContentController *userContentController = [[WKUserContentController alloc] init];

    //获取cookie
    NSArray *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies;
    NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];
    NSMutableString *cookieValue = [NSMutableString string];
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for(NSHTTPCookie *cookie in [cookieStorage cookies]) {
    [cookieDic setObject:cookie.value forKey:cookie.name];
    }
    //cookie去重复,先放到字典中再去重
    for(NSString *key in cookieDic.allKeys){
    NSString *appendingStr = [NSString stringWithFormat:@"%@=%@",key,[cookieDic valueForKey:key]];
    [cookieValue appendString:appendingStr];
    }

    //js注入cookie,防止从请求页面返回后再次请求页面失败,ios 11以前系统
    NSString *cookieSource = [[@"document.cookie = '" stringByAppendingString:cookieValue] stringByAppendingString:@"'"];
    WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource:cookieSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    [userContentController addUserScript:cookieScript];

    //测试将参数保存到缓存localStorage里,方便后台调用。
    NSString *sendTocen = [NSString stringWithFormat:@"localStorage.setItem("accessToken",'%@');localStorage.setItem("testItem",'%@');",[self getUserName],[self getUserSP]];
    //设置js和oc交互。可以设置多个
    WKUserScript *script = [[WKUserScript alloc] initWithSource:sendTocen injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    [userContentController addScriptMessageHandler:[WeakScriptMessageDelegate alloc] name:@"iOS"];
    [userContentController addUserScript:script];

    //WkwebView 配置协议
    WKWebViewConfiguration *config = [WKWebViewConfiguration new];
    config.userContentController = userContentController;

    config.preferences.javaScriptEnabled = YES;
    config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.suppressesIncrementalRendering = YES; // 是否支持记忆读取
    [config.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    if (@available(iOS 10.0, *)) {
    [config setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
    }
    //定义WKWebView
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    //设置代理
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;

//拼接访问地址
NSString *urlString = "自己的url";

// NSString *urlString = [baseUrl stringByAppendingString:@"JIRA/test.jsp"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:url];
//设置请求头
[request setValue:cookieValue forHTTPHeaderField:@"Cookie"];
//设置cookie
// NSDictionary *requestHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
// request.allHTTPHeaderFields = requestHeaderFields;
//设置post请求
// [request setHTTPMethod:@"POST"];
//加载后台页面
[_webView loadRequest:request];

//将导航栏设置为透明
self.navigationController.navigationBar.translucent = YES;
[self.view addSubview:_webView];
//重写返回按钮
self.navigationItem.leftBarButtonItem = self.backItem;

}

/**

*重写返回按钮,可以是页面返回上一页

*/
-(UIBarButtonItem *)backItem{

if (!self.backItem0) {
    UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];
    [back setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
    //        [back setTitle:@"Back" forState:UIControlStateNormal];
    back.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    back.titleLabel.font = [UIFont systemFontOfSize:17];
    back.frame = CGRectMake(0, 0, 44, 32);
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventAllEvents];
    self.backItem0 = [[UIBarButtonItem alloc] initWithCustomView:back];
   
}

return self.backItem0;

}

-(void)back{
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(toDoSomeThing: ) object:self.btn];

[self performSelector:@selector(toDoSomeThing:) withObject:self.btn afterDelay:0.8f];

}

-(void)toDoSomeThing:(UIViewController *)vc{

if ([self.webView canGoBack]) {
    
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                    appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏
    //切换到竖屏
    [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
    [self.webView goBack];
    
}else{
    
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏
    //切换到竖屏
    [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
    
    [self.view resignFirstResponder];
    //返回主菜单原生页面的时候导航栏设置为不透明
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

}

//解决页面第一访问后cookie丢失问题

  • (void)webView:(WKWebView)webView decidePolicyForNavigationResponse:(WKNavigationResponse)navigationResponse decisionHandler:(void(^)(WKNavigationResponsePolicy))decisionHandler{

    NSArray *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies;

    if (@available(iOS 11.0, *)) {
    WKHTTPCookieStore *cookieStroe = webView.configuration.websiteDataStore.httpCookieStore;
    for(NSHTTPCookie *cookie in cookies) {
    [cookieStroe setCookie:cookie completionHandler:nil];
    }
    }

    decisionHandler(WKNavigationResponsePolicyAllow);

}

pragma mark - WKNavigationDelegate

// 页面开始加载时调用

  • (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{

    if (self.loadingAlert==nil){
    self.loadingAlert = [[UIAlertView alloc] initWithTitle:nil
    message: @"正在加载数据,请稍候....."
    delegate: self
    cancelButtonTitle: nil
    otherButtonTitles: nil];
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityView.frame = CGRectMake(120.f, 48.0f, 37.0f, 37.0f);
    [self.loadingAlert addSubview:activityView];
    [activityView startAnimating];
    [self.loadingAlert show];
    }

}

// 当内容开始返回时调用

  • (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{

}
// 页面加载完成之后调用

  • (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{

    [self.loadingAlert dismissWithClickedButtonIndex:0 animated:YES];

}

//页面加载失败时调用

  • (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
    {
    NSLog(@"加载失败%@", error.userInfo);
    }

// 接收到服务器跳转请求之后调用

  • (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{

}

/**

*自建证书没有得到认证,访问https时需要强制信任证书,不知道我理解的有没有错
*/

  • (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *_Nullable))completionHandler
    {
    // NSLog(@"=====证书pppp=======");
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
    if (challenge.previousFailureCount == 0) {
    NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
    } else {
    completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
    }
    }
    }

/**

  • 将用户名和密码放到浏览器缓存里,这个是不需要的,懒得该

*/

  • (NSString *)getUserName {

    return uname;
    }

  • (NSString *)getUserSP {

return usersp01;

}

/**

  • web界面中有弹出警告框时调用
  • @param webView 实现该代理的webview
  • @param message 警告框中的内容
  • @param completionHandler 警告框消失调用
    */
  • (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    //将本地加载等待弹框清除
    [self.loadingAlert dismissWithClickedButtonIndex:0 animated:YES];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:([UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

      if([message isEqualToString:@"用户信息过期,请重新登录!"]){
          LogInViewController *loginVC = [[LogInViewController alloc] init];
    
          AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
          IdleWindow *idleWindow = (IdleWindow *)delegate.window; //这个是我写的记录手机时间超时的
    

// [[[NSURLCache alloc] init] removeAllCachedResponses]; //清除NSURLCache的缓存
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[loginVC clearCookiesForWkWebView];
[idleWindow setRootViewController:loginVC];
}
completionHandler();
}])];
[self presentViewController:alertController animated:YES completion:nil];
}

/**

  • 清除WKWebView 的cookie
    */
    -(void)clearCookiesForWkWebView{

    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0 ){
    NSArray *types = @[WKWebsiteDataTypeCookies,WKWebsiteDataTypeSessionStorage];
    NSSet *websiteDataTypes = [NSSet setWithArray:types];
    NSDate *dateformter = [NSDate dateWithTimeIntervalSince1970:0];
    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateformter completionHandler:^{

     }];
    

    }else{
    NSString *libaryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *cookieFolderPath = [libaryPath stringByAppendingString:@"/Cookies"];
    NSError *errors;
    [[NSFileManager defaultManager] removeItemAtPath:cookieFolderPath error:&errors];
    }
    }

// 确认框
//JavaScript调用confirm方法后回调的方法 confirm是js中的确定框,需要在block中把用户选择的情况传递进去

  • (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:([UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    completionHandler(NO);
    }])];
    [alertController addAction:([UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    completionHandler(YES);
    }])];
    [self presentViewController:alertController animated:YES completion:nil];
    }

// 输入框
//JavaScript调用prompt方法后回调的方法 prompt是js中的输入框 需要在block中把用户输入的信息传入

  • (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.text = defaultText;
    }];
    [alertController addAction:([UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    completionHandler(alertController.textFields[0].text?:@"");
    }])];
    [self presentViewController:alertController animated:YES completion:nil];
    }

// 页面是弹出窗口 _blank 处理

  • (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
    if (!navigationAction.targetFrame.isMainFrame) {
    [webView loadRequest:navigationAction.request];
    }
    return nil;
    }

@end

@implementation WeakScriptMessageDelegate

  • (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)delegate {
    self = [super init];
    if (self) {
    _delegate = delegate;
    }
    return self;
    }

  • (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {

    if (self.delegate && [self.delegate respondsToSelector:@selector(userContentController:didReceiveScriptMessage:)]) {
    [self.delegate userContentController:userContentController didReceiveScriptMessage:message];
    }
    }

@end

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342

推荐阅读更多精彩内容