Skip to content

Commit 88c9437

Browse files
committed
add tel&sms support
1.2.2
1 parent db8e7a5 commit 88c9437

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ source 'https://github.com/CocoaPods/Specs.git'
2020
platform :ios, '9.0'
2121
2222
target 'TargetName' do
23-
pod 'ZFWKWebView', '~> 1.2.1'
23+
pod 'ZFWKWebView', '~> 1.2.2'
2424
end
2525
```
2626
Then, run the following command:

ZFWKWebView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
Pod::Spec.new do |s|
1111
s.name = "ZFWKWebView"
12-
s.version = "1.2.1"
12+
s.version = "1.2.2"
1313
s.summary = "No short description of ZFWKWebView."
1414
s.homepage = "https://github.com/FranLucky/ZFWKWebView"
1515
s.license = { :type => "MIT", :file => "LICENSE"}

ZFWKWebView/ZFWKWebVC.m

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,39 +277,44 @@ - (instancetype)initWithConf:(ZFWKWebVCConf *)conf {
277277
}
278278

279279
- (void)addJS {
280-
[self.webView.configuration.userContentController removeAllUserScripts];
281-
for (NSString *methodName in self.config.callbacks.allKeys) {
282-
[self.webView.configuration.userContentController addScriptMessageHandler:self name:methodName];
283-
}
280+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
281+
for (NSString *methodName in self.config.callbacks.allKeys) {
282+
[self.webView.configuration.userContentController addScriptMessageHandler:self name:methodName];
283+
}
284+
});
284285
}
286+
285287
- (void)removeJS {
286-
for (NSString *name in self.config.callbacks.allKeys) {
287-
[self.webView.configuration.userContentController removeScriptMessageHandlerForName:name];
288-
}
288+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
289+
for (NSString *name in self.config.callbacks.allKeys) {
290+
[self.webView.configuration.userContentController removeScriptMessageHandlerForName:name];
291+
}
292+
});
289293
}
294+
290295
- (void)viewWillAppear:(BOOL)animated {
291296
[super viewWillAppear:animated];
292-
293297
zf_wkWebViewEventCallBack callback = self.config.callbacks[ZFWKWebViewEventViewWillAppear];
294298
if (callback) callback(self, self.config, nil);
295-
299+
[self removeJS];
296300
[self addJS];
297301
}
302+
298303
- (void)viewWillDisappear:(BOOL)animated {
299304
[super viewWillDisappear:animated];
300-
301305
zf_wkWebViewEventCallBack callback = self.config.callbacks[ZFWKWebViewEventViewWillDisappear];
302306
if (callback) callback(self, self.config, nil);
303-
304307
[self removeJS];
305308
}
309+
306310
- (void)viewDidDisappear:(BOOL)animated {
307311
[super viewDidDisappear:animated];
308312

309313
zf_wkWebViewEventCallBack callback = self.config.callbacks[ZFWKWebViewEventViewDidDisappear];
310314
if (callback) callback(self, self.config, nil);
311315

312316
}
317+
313318
- (void)viewDidLoad {
314319
[super viewDidLoad];
315320
if (self.navigationController.navigationBar) self.navigationController.navigationBar.hidden = YES;
@@ -726,7 +731,24 @@ - (void)showError:(NSError *)error {
726731
}
727732

728733
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
729-
decisionHandler(WKNavigationActionPolicyAllow);
734+
NSURL *currentUrl = navigationAction.request.URL;
735+
NSString *path = [currentUrl absoluteString].lowercaseString;
736+
NSLog(@"path:%@", path);
737+
if ([path hasPrefix:@"tel:"] ||
738+
[path hasPrefix:@"telprompt:"] ||
739+
[path hasPrefix:@"sms:"]) {
740+
dispatch_async(dispatch_get_main_queue(), ^{
741+
if ([[UIApplication sharedApplication] canOpenURL:currentUrl]) {
742+
[[UIApplication sharedApplication] openURL:currentUrl];
743+
decisionHandler(WKNavigationActionPolicyCancel);
744+
} else {
745+
decisionHandler(WKNavigationActionPolicyAllow);
746+
}
747+
});
748+
} else {
749+
decisionHandler(WKNavigationActionPolicyAllow);
750+
}
751+
730752
}
731753

732754

sample/ZFWKWebView.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
7D9E7165234D6F9800F9BEE4 /* ZFWKWebVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D9E7162234D6F9800F9BEE4 /* ZFWKWebVC.m */; };
1818
7D9E7168234D709000F9BEE4 /* ZFWKUserDefaultConf.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D9E7167234D709000F9BEE4 /* ZFWKUserDefaultConf.m */; };
1919
7DC5CEA92361703A00D307FE /* ZFWKWebViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DC5CEA82361703A00D307FE /* ZFWKWebViewTests.m */; };
20+
7DE872382433091F0069C6C3 /* temp.html in Resources */ = {isa = PBXBuildFile; fileRef = 7DE872372433091F0069C6C3 /* temp.html */; };
2021
/* End PBXBuildFile section */
2122

2223
/* Begin PBXContainerItemProxy section */
@@ -50,6 +51,7 @@
5051
7DC5CEA62361703A00D307FE /* ZFWKWebViewTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ZFWKWebViewTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5152
7DC5CEA82361703A00D307FE /* ZFWKWebViewTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ZFWKWebViewTests.m; sourceTree = "<group>"; };
5253
7DC5CEAA2361703A00D307FE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
54+
7DE872372433091F0069C6C3 /* temp.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = temp.html; sourceTree = "<group>"; };
5355
/* End PBXFileReference section */
5456

5557
/* Begin PBXFrameworksBuildPhase section */
@@ -96,6 +98,7 @@
9698
7D9E7142234D6EE900F9BEE4 /* AppDelegate.m */,
9799
7D9E7147234D6EE900F9BEE4 /* ViewController.h */,
98100
7D9E7148234D6EE900F9BEE4 /* ViewController.m */,
101+
7DE872372433091F0069C6C3 /* temp.html */,
99102
7D9E7166234D709000F9BEE4 /* ZFWKUserDefaultConf.h */,
100103
7D9E7167234D709000F9BEE4 /* ZFWKUserDefaultConf.m */,
101104
7D9E714A234D6EE900F9BEE4 /* Main.storyboard */,
@@ -211,6 +214,7 @@
211214
7D9E7151234D6EEA00F9BEE4 /* LaunchScreen.storyboard in Resources */,
212215
7D9E714E234D6EEA00F9BEE4 /* Assets.xcassets in Resources */,
213216
7D9E714C234D6EE900F9BEE4 /* Main.storyboard in Resources */,
217+
7DE872382433091F0069C6C3 /* temp.html in Resources */,
214218
7D9E7164234D6F9800F9BEE4 /* ImageResource.bundle in Resources */,
215219
);
216220
runOnlyForDeploymentPostprocessing = 0;

sample/ZFWKWebView/ViewController.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ - (void)buttonClicked:(UIButton *)button {
5252

5353
} else {
5454
ZFWKWebVC *web = [[ZFWKWebVC alloc] initWithDefaultConfig];
55-
[web.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://baidu.com"]]];
55+
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"temp.html" withExtension:nil];
56+
[web.webView loadFileURL:fileUrl allowingReadAccessToURL:fileUrl];
57+
58+
// [web.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://baidu.com"]]];
5659
[self.navigationController pushViewController:web animated:YES];
5760
}
5861

sample/ZFWKWebView/temp.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<a href="sms://4001-086-086">打电话</a>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)