|
@@ -34,9 +34,9 @@
|
|
|
@property (strong, nonatomic) WKWebView *wkwebView;
|
|
@property (strong, nonatomic) WKWebView *wkwebView;
|
|
|
@property (nonatomic,strong) NSDictionary *currentParams;
|
|
@property (nonatomic,strong) NSDictionary *currentParams;
|
|
|
|
|
|
|
|
-@property (nonatomic,assign) BOOL initialize;///< 是否已经加载了主页
|
|
|
|
|
@property (nonatomic,weak) RAProgressHUD *hud;
|
|
@property (nonatomic,weak) RAProgressHUD *hud;
|
|
|
@property (nonatomic,copy) NSString *url;
|
|
@property (nonatomic,copy) NSString *url;
|
|
|
|
|
+@property (nonatomic,assign) BOOL firstLoad;
|
|
|
|
|
|
|
|
@end
|
|
@end
|
|
|
|
|
|
|
@@ -87,16 +87,19 @@
|
|
|
[self addSubview:self.wkwebView];
|
|
[self addSubview:self.wkwebView];
|
|
|
|
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
__weak typeof(self) weakSelf = self;
|
|
|
- self.wkwebView.scrollView.emptyView = [RAEmptyView emptyViewWithTapBlk:^(id sender) {
|
|
|
|
|
|
|
+ [self.wkwebView.scrollView setEmptyContentViewBackgroupColor:UIColor.whiteColor];
|
|
|
|
|
+ self.wkwebView.scrollView.emptyView = [RAEmptyView emptyViewWithTitle:@"can not reach the server.\ntap to reload" clickHandler:^(id sender) {
|
|
|
[weakSelf LoadFromURL:weakSelf.url];
|
|
[weakSelf LoadFromURL:weakSelf.url];
|
|
|
}];
|
|
}];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ self.firstLoad = YES;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-(void)LoadFromURL:(NSString *)url
|
|
-(void)LoadFromURL:(NSString *)url
|
|
|
{
|
|
{
|
|
|
if(self.wkwebView==nil)
|
|
if(self.wkwebView==nil)
|
|
|
return;
|
|
return;
|
|
|
- self.url = url;
|
|
|
|
|
NSURL* U=[NSURL URLWithString:url];
|
|
NSURL* U=[NSURL URLWithString:url];
|
|
|
NSURLRequest* request=[[NSURLRequest alloc]initWithURL:U cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0f];
|
|
NSURLRequest* request=[[NSURLRequest alloc]initWithURL:U cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0f];
|
|
|
|
|
|
|
@@ -120,6 +123,7 @@
|
|
|
[self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Signature"];
|
|
[self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Signature"];
|
|
|
[self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"SelectContact"];
|
|
[self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"SelectContact"];
|
|
|
[self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"requestApplicationVersion"];
|
|
[self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"requestApplicationVersion"];
|
|
|
|
|
+ [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"cleanCache"];
|
|
|
// [self.configuration.userContentController addScriptMessageHandler:self name:@"Color"];
|
|
// [self.configuration.userContentController addScriptMessageHandler:self name:@"Color"];
|
|
|
// [self.configuration.userContentController addScriptMessageHandler:self name:@"Pay"];
|
|
// [self.configuration.userContentController addScriptMessageHandler:self name:@"Pay"];
|
|
|
// [self.configuration.userContentController addScriptMessageHandler:self name:@"Shake"];
|
|
// [self.configuration.userContentController addScriptMessageHandler:self name:@"Shake"];
|
|
@@ -142,6 +146,7 @@
|
|
|
[self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Signature"];
|
|
[self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Signature"];
|
|
|
[self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"SelectContact"];
|
|
[self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"SelectContact"];
|
|
|
[self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"requestApplicationVersion"];
|
|
[self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"requestApplicationVersion"];
|
|
|
|
|
+ [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"cleanCache"];
|
|
|
}
|
|
}
|
|
|
- (void)dealloc
|
|
- (void)dealloc
|
|
|
{
|
|
{
|
|
@@ -576,6 +581,52 @@
|
|
|
[self returnToWebPage:params value:appVer];
|
|
[self returnToWebPage:params value:appVer];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+- (void)ra_cleanCache:(NSDictionary *)params {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+
|
|
|
|
|
+ // On-disk caches.
|
|
|
|
|
+ WKWebsiteDataTypeDiskCache API_AVAILABLE(macosx(10.11), ios(9.0));
|
|
|
|
|
+
|
|
|
|
|
+ // In-memory caches.
|
|
|
|
|
+ WKWebsiteDataTypeMemoryCache API_AVAILABLE(macosx(10.11), ios(9.0));
|
|
|
|
|
+
|
|
|
|
|
+ // HTML offline web application caches.
|
|
|
|
|
+ WKWebsiteDataTypeOfflineWebApplicationCache API_AVAILABLE(macosx(10.11), ios(9.0));
|
|
|
|
|
+
|
|
|
|
|
+ // Cookies.
|
|
|
|
|
+ WKWebsiteDataTypeCookies API_AVAILABLE(macosx(10.11), ios(9.0));
|
|
|
|
|
+
|
|
|
|
|
+ // HTML session storage.
|
|
|
|
|
+ WKWebsiteDataTypeSessionStorage API_AVAILABLE(macosx(10.11), ios(9.0));
|
|
|
|
|
+
|
|
|
|
|
+ // HTML local storage.
|
|
|
|
|
+ WKWebsiteDataTypeLocalStorage API_AVAILABLE(macosx(10.11), ios(9.0));
|
|
|
|
|
+
|
|
|
|
|
+ // WebSQL databases.
|
|
|
|
|
+ WKWebsiteDataTypeWebSQLDatabases API_AVAILABLE(macosx(10.11), ios(9.0));
|
|
|
|
|
+
|
|
|
|
|
+ // IndexedDB databases.
|
|
|
|
|
+ WKWebsiteDataTypeIndexedDBDatabases API_AVAILABLE(macosx(10.11), ios(9.0));
|
|
|
|
|
+
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+ NSArray *typeArr = @[
|
|
|
|
|
+ WKWebsiteDataTypeDiskCache,
|
|
|
|
|
+ WKWebsiteDataTypeMemoryCache,
|
|
|
|
|
+ WKWebsiteDataTypeOfflineWebApplicationCache,
|
|
|
|
|
+ WKWebsiteDataTypeLocalStorage,
|
|
|
|
|
+ WKWebsiteDataTypeWebSQLDatabases,
|
|
|
|
|
+ WKWebsiteDataTypeIndexedDBDatabases
|
|
|
|
|
+ ];
|
|
|
|
|
+ NSSet *typeSet = [NSSet setWithArray:typeArr];
|
|
|
|
|
+ NSDate *dateSince = [NSDate dateWithTimeIntervalSince1970:0];
|
|
|
|
|
+
|
|
|
|
|
+ [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:typeSet modifiedSince:dateSince completionHandler:^{
|
|
|
|
|
+ [self returnToWebPage:params value:@""];
|
|
|
|
|
+ }];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
#pragma mark - Utils
|
|
#pragma mark - Utils
|
|
|
|
|
|
|
|
- (NSString *)adjustNilString:(NSString *)str {
|
|
- (NSString *)adjustNilString:(NSString *)str {
|
|
@@ -774,6 +825,9 @@
|
|
|
} else if ([message.name isEqualToString:@"requestApplicationVersion"]) {
|
|
} else if ([message.name isEqualToString:@"requestApplicationVersion"]) {
|
|
|
|
|
|
|
|
[self handleRequestApplicationVersion:params];
|
|
[self handleRequestApplicationVersion:params];
|
|
|
|
|
+ } else if ([message.name isEqualToString:@"cleanCache"]) {
|
|
|
|
|
+
|
|
|
|
|
+ [self handleCleanCache:params];
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -895,14 +949,12 @@
|
|
|
[self ra_requestAppVersion:params];
|
|
[self ra_requestAppVersion:params];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#pragma mark - WKNavigationDelegate
|
|
|
|
|
-
|
|
|
|
|
-- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
|
|
|
|
|
- if (!self.initialize) {
|
|
|
|
|
- self.hud = [RAProgressHUD showHUDOnView:self.ra_viewController.view];
|
|
|
|
|
- }
|
|
|
|
|
|
|
+- (void)handleCleanCache:(NSDictionary *)params {
|
|
|
|
|
+ [self ra_cleanCache:params];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+#pragma mark - WKNavigationDelegate
|
|
|
|
|
+
|
|
|
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
|
|
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
|
|
|
NSLog(@"%s",__func__);
|
|
NSLog(@"%s",__func__);
|
|
|
}
|
|
}
|
|
@@ -948,34 +1000,47 @@
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
|
|
|
|
|
|
|
+
|
|
|
|
|
+- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
|
|
|
|
|
+{
|
|
|
|
|
+ NSLog(@"%@",error);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+//禁止链接跳转
|
|
|
|
|
+- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
|
|
|
|
|
+
|
|
|
NSLog(@"%s",__func__);
|
|
NSLog(@"%s",__func__);
|
|
|
- NSLog(@"error: %@", error.localizedDescription);
|
|
|
|
|
- if (self.hud) {
|
|
|
|
|
- [self.hud dismiss];
|
|
|
|
|
- self.hud = nil;
|
|
|
|
|
- }
|
|
|
|
|
- __weak typeof(self) weakSelf = self;
|
|
|
|
|
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
- UIAlertAction *retryAction = [UIAlertAction actionWithTitle:@"Retry" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
|
|
- [weakSelf LoadFromURL:weakSelf.url];
|
|
|
|
|
- }];
|
|
|
|
|
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
|
|
- [self.wkwebView.scrollView showEmpty];
|
|
|
|
|
- }];
|
|
|
|
|
|
|
+ NSLog(@"navigationAction = %@, \r!!!!!!!!!!!!!!!!!!!!!!\n request = %@",navigationAction,navigationAction.request.URL);
|
|
|
|
|
|
|
|
- [alertVC addAction:okAction];
|
|
|
|
|
- [alertVC addAction:retryAction];
|
|
|
|
|
|
|
+ NSString *url = navigationAction.request.URL.absoluteString;
|
|
|
|
|
+ if (![url containsString:[NSBundle mainBundle].bundleURL.absoluteString]) {
|
|
|
|
|
+ self.url = url;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- [self.ra_viewController presentViewController:alertVC animated:YES completion:nil];
|
|
|
|
|
|
|
+ decisionHandler(WKNavigationActionPolicyAllow);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
|
|
|
|
|
|
|
+- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
|
|
|
NSLog(@"%s",__func__);
|
|
NSLog(@"%s",__func__);
|
|
|
|
|
+ [self.wkwebView.scrollView hideEmpty];
|
|
|
|
|
+ if (self.firstLoad) {
|
|
|
|
|
+ self.hud = [RAProgressHUD showHUDOnView:self.ra_viewController.view];
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
|
|
|
|
|
|
|
+- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
|
|
|
NSLog(@"%s",__func__);
|
|
NSLog(@"%s",__func__);
|
|
|
|
|
+ NSLog(@"error: %@", error.localizedDescription);
|
|
|
|
|
+ if (self.hud) {
|
|
|
|
|
+ [self.hud dismiss];
|
|
|
|
|
+ self.hud = nil;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
|
|
|
|
|
+// [webView loadHTMLString:@"<html></html>" baseURL:bundleURL];
|
|
|
|
|
+
|
|
|
|
|
+ [self.wkwebView.scrollView showEmpty];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
|
|
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
|
|
@@ -983,42 +1048,27 @@
|
|
|
if (self.hud) {
|
|
if (self.hud) {
|
|
|
[self.hud dismiss];
|
|
[self.hud dismiss];
|
|
|
self.hud = nil;
|
|
self.hud = nil;
|
|
|
- self.initialize = YES;
|
|
|
|
|
|
|
+ self.firstLoad = NO;
|
|
|
}
|
|
}
|
|
|
-// self.wkwebView.scrollView.scrollEnabled = NO;
|
|
|
|
|
- [webView evaluateJavaScript:@"document.body.scrollHeight"
|
|
|
|
|
- completionHandler:^(id result, NSError *_Nullable error) {
|
|
|
|
|
-
|
|
|
|
|
- NSLog(@"web page height:%@",result);
|
|
|
|
|
- //result 就是加载完成后 webView的实际高度
|
|
|
|
|
- //获取后返回重新布局
|
|
|
|
|
- }];
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // self.wkwebView.scrollView.scrollEnabled = NO;
|
|
|
|
|
+ [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id result, NSError *_Nullable error) {
|
|
|
|
|
+
|
|
|
|
|
+ NSLog(@"web page height:%@",result);
|
|
|
|
|
+ //result 就是加载完成后 webView的实际高度
|
|
|
|
|
+ //获取后返回重新布局
|
|
|
|
|
+
|
|
|
|
|
+ }];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
|
|
|
|
|
-{
|
|
|
|
|
- NSLog(@"%@",error);
|
|
|
|
|
|
|
+- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
|
|
|
|
|
+ NSLog(@"%s",__func__);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-//禁止链接跳转
|
|
|
|
|
-- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
|
|
|
|
|
-
|
|
|
|
|
- NSLog(@"navigationAction = %@, \r!!!!!!!!!!!!!!!!!!!!!!\n request = %@",navigationAction,navigationAction.request.URL);
|
|
|
|
|
-
|
|
|
|
|
-// if ([navigationAction.request.URL.scheme containsString:@"http"]) {
|
|
|
|
|
-// decisionHandler(WKNavigationActionPolicyCancel);
|
|
|
|
|
-// } else {
|
|
|
|
|
-
|
|
|
|
|
-// navigationAction.sourceFrame.
|
|
|
|
|
- decisionHandler(WKNavigationActionPolicyAllow);
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
|
|
|
|
|
+ NSLog(@"%s",__func__);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
|
|
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
|
|
|
{
|
|
{
|
|
|
NSLog(@"%s",__func__);
|
|
NSLog(@"%s",__func__);
|