Skip to content

Commit d013f1f

Browse files
committed
Merge branch 'fixBugs'
# Conflicts: # README.md # README_CN.md # Source/WebView/TWebView.h # Source/WebView/TWebView.m # Source/WebViewDelegate/TUIWebViewDelegate.m # TWebKit.podspec
2 parents 4e0ca37 + 734a21a commit d013f1f

6 files changed

Lines changed: 39 additions & 11 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ Most of the parameters and methods are same like UIWebView, the following descri
162162

163163
> Get cookies in `NSHTTPCookieStorage` and set the cookie for TWebView, the `forceOverride` parameter control use the cookie value in `NSHTTPCookieStorage` to reset the cookie of the same name that existed before TWebView, if `forceOverride` is `NO/false`, will don't reset the same name cookie.
164164
165+
- `- (void)getDocumentTitle:(void (^)(NSString * _Nullable))completion`
166+
167+
> Take out the page's `title` to `completion`(it was use `JavaScript` to get the web page in the `document.title`)
168+
165169
- `+ (nullable NSString *)getJavascriptStringWithFunctionName:(NSString *)function data:(id)data`
166170

167171
> Class method to get `JavaScript function`, `function` parameter to access the JavaScript method name (no need to add brackets), `data` parameters can be `JSON Object` or ordinary `NSString`, will automatically convert Stitching; returns a function call string after splicing.

README_CN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ github "tobedefined/TWebKit" ~> 1.2.1
163163

164164
> 取出`NSHTTPCookieStorage`中的cookies设置TWebView的cookie,`forceOverride`参数控制是否强制使用`NSHTTPCookieStorage`中的cookie值重设TWebView之前存在的同名的cookie
165165
166+
- `- (void)getDocumentTitle:(void (^)(NSString * _Nullable))completion`
167+
168+
> 取出网页的`title`放入block `completion`,使用`JavaScript`获得网页HTML中的`document.title`
169+
166170
- `+ (nullable NSString *)getJavascriptStringWithFunctionName:(NSString *)function data:(id)data`
167171

168172
> 类方法提供拼接JavaScript函数功能,`function`参数为要访问的JavaScript的方法名(不需要添加括号),`data`参数可以为`JSON Object`或者为普通的`NSString`,会自动进行转换拼接;返回拼接后的函数调用字符串。

Source/WebView/TWebView.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,16 @@ typedef NS_ENUM(NSUInteger, TWebViewLoadStatus) {
106106
// this forceOverride just once valid
107107
- (void)resetCookieForceOverride:(BOOL)forceOverride;
108108

109+
- (void)getDocumentTitle:(void (^)(NSString * _Nullable title))completion;
110+
109111
// 9.0以及之后,8.0之前可用
110112
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
111113
// 9.0之后可用
112114
- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL API_AVAILABLE(ios(9.0));
113115

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

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

118120
@end
119121

Source/WebView/TWebView.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,19 @@ - (void)resetCookieForceOverride:(BOOL)forceOverride {
410410
}];
411411
}
412412

413+
414+
- (void)getDocumentTitle:(void (^)(NSString * _Nullable))completion {
415+
[self runJavascript:@"document.title" completion:^(id _Nullable obj, NSError * _Nullable error) {
416+
if (error != nil) {
417+
TLog(@"%@", error);
418+
}
419+
if ([obj isKindOfClass:[NSString class]]) {
420+
completion((NSString *)obj);
421+
} else {
422+
completion(nil);
423+
}
424+
}];
425+
}
413426
#pragma mark - Get Delegate
414427
- (nullable id<TWebViewDelegate>)getDelegateWithSEL:(SEL)sel {
415428
if ([self.delegate respondsToSelector:sel]) {

Source/WebViewDelegate/TUIWebViewDelegate.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
118118
withError:error];
119119

120120
delegate = [self.tWebView getDelegateWithSEL:@selector(webView:loadStatus:title:)];
121-
[delegate webView:self.tWebView
122-
loadStatus:TWebViewLoadStatusFailed
123-
title:self.tWebView.failedDefaultTitle];
124-
121+
[self.tWebView getDocumentTitle:^(NSString * _Nullable title) {
122+
[delegate webView:self.tWebView
123+
loadStatus:TWebViewLoadStatusFailed
124+
title:isNotEmptyString(title) ? title : self.tWebView.failedDefaultTitle];
125+
}];
125126
[self reduceLoadingCount:error];
126127
}
127128

Source/WebViewDelegate/TWKWebViewDelegate.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigatio
9797
[delegate webView:self.tWebView didFailedLoadRequest:self.tWebView.request withError:error];
9898

9999
delegate = [self.tWebView getDelegateWithSEL:@selector(webView:loadStatus:title:)];
100-
[delegate webView:self.tWebView
101-
loadStatus:TWebViewLoadStatusFailed
102-
title:self.tWebView.failedDefaultTitle];
100+
[self.tWebView getDocumentTitle:^(NSString * _Nullable title) {
101+
[delegate webView:self.tWebView
102+
loadStatus:TWebViewLoadStatusFailed
103+
title:isNotEmptyString(title) ? title : self.tWebView.failedDefaultTitle];
104+
}];
103105
}
104106

105107
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
@@ -111,9 +113,11 @@ - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation
111113
[delegate webView:self.tWebView didFailedLoadRequest:self.tWebView.request withError:error];
112114

113115
delegate = [self.tWebView getDelegateWithSEL:@selector(webView:loadStatus:title:)];
114-
[delegate webView:self.tWebView
115-
loadStatus:TWebViewLoadStatusFailed
116-
title:self.tWebView.failedDefaultTitle];
116+
[self.tWebView getDocumentTitle:^(NSString * _Nullable title) {
117+
[delegate webView:self.tWebView
118+
loadStatus:TWebViewLoadStatusFailed
119+
title:isNotEmptyString(title) ? title : self.tWebView.failedDefaultTitle];
120+
}];
117121
}
118122

119123

0 commit comments

Comments
 (0)