Skip to content

Commit

Permalink
Merge branch 'fixBugs'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	README_CN.md
#	Source/WebView/TWebView.h
#	Source/WebView/TWebView.m
#	Source/WebViewDelegate/TUIWebViewDelegate.m
#	TWebKit.podspec
  • Loading branch information
ToBeDefined committed Oct 15, 2017
2 parents 4e0ca37 + 734a21a commit d013f1f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ Most of the parameters and methods are same like UIWebView, the following descri

> 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.
- `- (void)getDocumentTitle:(void (^)(NSString * _Nullable))completion`

> Take out the page's `title` to `completion`(it was use `JavaScript` to get the web page in the `document.title`)
- `+ (nullable NSString *)getJavascriptStringWithFunctionName:(NSString *)function data:(id)data`

> 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.
Expand Down
4 changes: 4 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ github "tobedefined/TWebKit" ~> 1.2.1

> 取出`NSHTTPCookieStorage`中的cookies设置TWebView的cookie,`forceOverride`参数控制是否强制使用`NSHTTPCookieStorage`中的cookie值重设TWebView之前存在的同名的cookie
- `- (void)getDocumentTitle:(void (^)(NSString * _Nullable))completion`

> 取出网页的`title`放入block `completion`,使用`JavaScript`获得网页HTML中的`document.title`
- `+ (nullable NSString *)getJavascriptStringWithFunctionName:(NSString *)function data:(id)data`

> 类方法提供拼接JavaScript函数功能,`function`参数为要访问的JavaScript的方法名(不需要添加括号),`data`参数可以为`JSON Object`或者为普通的`NSString`,会自动进行转换拼接;返回拼接后的函数调用字符串。
Expand Down
4 changes: 3 additions & 1 deletion Source/WebView/TWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,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 @@ -410,6 +410,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 @@ -97,9 +97,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 @@ -111,9 +113,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 d013f1f

Please sign in to comment.