WKWebView 使用

2020-11-10  本文已影响0人  枯竹流过岁月

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

}

//拼接访问地址
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丢失问题

}

pragma mark - WKNavigationDelegate

// 页面开始加载时调用

}

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

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

}

//页面加载失败时调用

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

}

/**

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

/**

*/

return usersp01;

}

/**

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

/**

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

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

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

@end

@implementation WeakScriptMessageDelegate

@end

上一篇下一篇

猜你喜欢

热点阅读