Skip to content

Commit

Permalink
修复返回上一页可能出现加载失败的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ToBeDefined committed Oct 15, 2017
1 parent f41fa11 commit 761e732
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Source/WebView/TWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ typedef NS_ENUM(NSUInteger, TWebViewLoadStatus) {
// this forceOverride just once valid
- (void)resetCookieForceOverride:(BOOL)forceOverride;

- (void)getDocumentTitle:(void (^)(NSString * _Nullable title))completion;

// 9.0以及之后,8.0之前可用
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
// 9.0之后可用
- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL API_AVAILABLE(ios(9.0));

+ (nullable NSString *)getJavascriptStringWithFunctionName:(NSString *)function data:(id)data;

- (void)runJavascript:(NSString *)js completion:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completion;
- (void)runJavascript:(NSString *)js completion:(void (^ _Nullable)(_Nullable id obj, NSError * _Nullable error))completion;

@end

Expand Down
13 changes: 13 additions & 0 deletions Source/WebView/TWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,19 @@ - (void)resetCookieForceOverride:(BOOL)forceOverride {
}];
}


- (void)getDocumentTitle:(void (^)(NSString * _Nullable))completion {
[self runJavascript:@"document.title" completion:^(id _Nullable obj, NSError * _Nullable error) {
if (error != nil) {
TLog(@"%@", error);
}
if ([obj isKindOfClass:[NSString class]]) {
completion((NSString *)obj);
} else {
completion(nil);
}
}];
}
#pragma mark - Get Delegate
- (nullable id<TWebViewDelegate>)getDelegateWithSEL:(SEL)sel {
if ([self.delegate respondsToSelector:sel]) {
Expand Down
9 changes: 5 additions & 4 deletions Source/WebViewDelegate/TUIWebViewDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
withError:error];

delegate = [self.tWebView getDelegateWithSEL:@selector(webView:loadStatus:title:)];
[delegate webView:self.tWebView
loadStatus:TWebViewLoadStatusFailed
title:self.tWebView.failedDefaultTitle];

[self.tWebView getDocumentTitle:^(NSString * _Nullable title) {
[delegate webView:self.tWebView
loadStatus:TWebViewLoadStatusFailed
title:isNotEmptyString(title) ? title : self.tWebView.failedDefaultTitle];
}];
[self reduceLoadingCount:error];
}

Expand Down
16 changes: 10 additions & 6 deletions Source/WebViewDelegate/TWKWebViewDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigatio
[delegate webView:self.tWebView didFailedLoadRequest:self.tWebView.request withError:error];

delegate = [self.tWebView getDelegateWithSEL:@selector(webView:loadStatus:title:)];
[delegate webView:self.tWebView
loadStatus:TWebViewLoadStatusFailed
title:self.tWebView.failedDefaultTitle];
[self.tWebView getDocumentTitle:^(NSString * _Nullable title) {
[delegate webView:self.tWebView
loadStatus:TWebViewLoadStatusFailed
title:isNotEmptyString(title) ? title : self.tWebView.failedDefaultTitle];
}];
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
Expand All @@ -108,9 +110,11 @@ - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation
[delegate webView:self.tWebView didFailedLoadRequest:self.tWebView.request withError:error];

delegate = [self.tWebView getDelegateWithSEL:@selector(webView:loadStatus:title:)];
[delegate webView:self.tWebView
loadStatus:TWebViewLoadStatusFailed
title:self.tWebView.failedDefaultTitle];
[self.tWebView getDocumentTitle:^(NSString * _Nullable title) {
[delegate webView:self.tWebView
loadStatus:TWebViewLoadStatusFailed
title:isNotEmptyString(title) ? title : self.tWebView.failedDefaultTitle];
}];
}


Expand Down

0 comments on commit 761e732

Please sign in to comment.