250 lines
7.8 KiB
Objective-C
250 lines
7.8 KiB
Objective-C
//
|
|
// XJBaseWebViewVC.m
|
|
// ScarHousekeeper
|
|
//
|
|
// Created by 张小万 on 2021/1/21.
|
|
//
|
|
|
|
#import "XJBaseWebViewVC.h"
|
|
|
|
@interface XJBaseWebViewVC ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
|
|
|
|
@property (nonatomic, strong)NSURL *originalUrl;
|
|
@property (nonatomic, copy)NSString *videoUrl;
|
|
@property (nonatomic, copy)NSString *messageType;
|
|
|
|
|
|
@end
|
|
|
|
@implementation XJBaseWebViewVC
|
|
|
|
|
|
#pragma mark - LifeCycle
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
self.view.backgroundColor = [UIColor whiteColor];
|
|
self.isHidenNaviBar = false;
|
|
[self addNavigationItemWithImageNames:@[@"whiteBack_icon"] isLeft:YES target:self action:@selector(backVC) tags:@[@""]];
|
|
[self addSubviews];
|
|
if (self.webTitle) {
|
|
self.navigationItem.title = self.webTitle;
|
|
}
|
|
}
|
|
|
|
- (void)backVC {
|
|
|
|
[self.navigationController popViewControllerAnimated:YES];
|
|
}
|
|
|
|
- (void)viewWillAppear:(BOOL)animated{
|
|
[super viewWillAppear:animated];
|
|
if (_wkWebView) {
|
|
_wkWebView.navigationDelegate = self;
|
|
_wkWebView.UIDelegate = self;
|
|
_wkWebView.allowsBackForwardNavigationGestures = true;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated{
|
|
[super viewWillDisappear:animated];
|
|
_wkWebView.navigationDelegate = nil;
|
|
_wkWebView.UIDelegate = nil;
|
|
//清除cookies
|
|
NSHTTPCookie *cookie;
|
|
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
|
|
for (cookie in [storage cookies]) {
|
|
[storage deleteCookie:cookie];
|
|
}
|
|
[[NSURLCache sharedURLCache] removeAllCachedResponses];
|
|
[self clearWebViewCache];
|
|
}
|
|
|
|
#pragma mark - Create UI
|
|
- (void)addSubviews{
|
|
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
|
|
WKPreferences *preferences = [[WKPreferences alloc]init];
|
|
// preferences.javaScriptEnabled = YES;
|
|
preferences.javaScriptCanOpenWindowsAutomatically = YES;
|
|
preferences.minimumFontSize = 10;
|
|
configuration.preferences = preferences;
|
|
[configuration.userContentController addScriptMessageHandler:self name:@"JSCallback"];
|
|
[configuration.userContentController addScriptMessageHandler:self name:@"htmlBack"];
|
|
//防止搜索框自动放大 注入js代码
|
|
WKUserContentController *userController = [WKUserContentController new];
|
|
NSString *jScript = @"var script = document.createElement('meta');script.name = 'viewport';script.content='width=device-width,user-scalable=no';document.getElementsByTagName('head')[0].appendChild(script);";
|
|
|
|
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
|
|
WKUserContentController * wkUController = [[WKUserContentController alloc] init];
|
|
[wkUController addScriptMessageHandler: self name: @"finishActivity"];//不带参数的调用
|
|
|
|
[wkUController addUserScript:wkUScript];
|
|
configuration.userContentController= wkUController;
|
|
|
|
self.wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, MyViewHEIGHT) configuration:configuration];
|
|
self.wkWebView.navigationDelegate = self;
|
|
self.wkWebView.UIDelegate = self;
|
|
if (@available(iOS 11.0, *)) {
|
|
self.wkWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
}
|
|
[self.view addSubview:self.wkWebView];
|
|
self.wkWebView.scrollView.bounces = false;//禁止滑动
|
|
[self webViewRequest];
|
|
/*
|
|
[self.wkWebView evaluateJavaScript:@"htmlBack" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
|
|
NSLog(@"response: %@ error: %@", response, error);
|
|
if (response) {
|
|
[self back];
|
|
}
|
|
}];
|
|
*/
|
|
|
|
|
|
}
|
|
|
|
- (void)finishActivity {
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)webViewRequest{
|
|
NSString *urlString = self.webUrl;
|
|
[self hiddenAllMBProgressHUD];
|
|
if (urlString.length > 0) {
|
|
[self showMBProgressHUDOnlyWithString:@"加载中"];
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
|
|
if (self.wkWebView) {
|
|
[self.wkWebView loadRequest:urlRequest];
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - WKNavigationDelegate
|
|
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
|
|
if ([message.name isEqualToString:@"JSCallback"]) {
|
|
if (message.body) {
|
|
NSData *jsonData = [message.body dataUsingEncoding:NSUTF8StringEncoding];
|
|
NSError *err;
|
|
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
|
|
options:NSJSONReadingMutableContainers
|
|
error:&err];
|
|
if(err) {
|
|
}
|
|
WkJSMessageType messageType = [[dic objectForKey:@"messageType"] intValue];
|
|
switch (messageType) {
|
|
case WkJSMessageTypeBack:{
|
|
// [self.navigationController popViewControllerAnimated:YES];
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
} else if ([message.name isEqualToString:@"htmlBack"]) {
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"fromWebToReloadData" object:nil];
|
|
}
|
|
}
|
|
|
|
#pragma mark - WKScriptMessageHandler
|
|
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
|
|
[self hiddenAllMBProgressHUD];
|
|
if (!navigationAction.targetFrame.isMainFrame) {
|
|
[webView loadRequest:navigationAction.request];
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
|
|
if (!self.webTitle) {
|
|
self.title = @"加载中...";
|
|
}
|
|
}
|
|
|
|
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
|
|
[self hiddenAllMBProgressHUD];
|
|
|
|
if (!self.webTitle) {
|
|
self.title = webView.title;
|
|
}
|
|
|
|
}
|
|
|
|
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(nonnull NSError *)error{
|
|
[self hiddenAllMBProgressHUD];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - ACTION
|
|
- (void)ToClickNavRight:(UIButton *)button{
|
|
|
|
}
|
|
|
|
|
|
#pragma mark - 自定义
|
|
- (void)onBackClicked{
|
|
if ( self.wkWebView.canGoBack){
|
|
[self.wkWebView goBack];
|
|
} else {
|
|
[self clearWebViewCache];
|
|
}
|
|
}
|
|
|
|
- (void)clearWebViewCache{
|
|
if (iOS9Later) {
|
|
NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
|
|
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
|
|
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
|
|
NSLog(@"-----clear webView cache success------");
|
|
}];
|
|
} else {
|
|
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
|
|
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
|
|
NSError *errors;
|
|
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
|
|
if (!errors) {
|
|
NSLog(@"-----clear wkWebView cache success------");
|
|
}
|
|
}
|
|
}
|
|
- (NSString *)URLEncodedString:(NSString *)urlString{
|
|
NSString *encodePath ;
|
|
if (!IOS7) {
|
|
encodePath = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
}else{
|
|
encodePath = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
|
|
}return encodePath;
|
|
}
|
|
|
|
|
|
#pragma mark - SETTER
|
|
- (void)setRightType:(RightNavType)rightType{
|
|
_rightType = rightType;
|
|
}
|
|
|
|
- (void)setWebUrl:(NSString *)webUrl{
|
|
_webUrl = webUrl;
|
|
[self webViewRequest];
|
|
}
|
|
|
|
|
|
@end
|