Skip to content

Commit d2b5235

Browse files
committed
完成iOS所有交互方式的Demo代码
1 parent 41ae4fb commit d2b5235

File tree

9 files changed

+165
-6
lines changed

9 files changed

+165
-6
lines changed

iOS/JsInterface.xcodeproj/project.pbxproj

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
2B2A0045200875F200AC8FA8 /* JSHomeTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2B2A0044200875F200AC8FA8 /* JSHomeTableViewCell.xib */; };
1313
2B39E35E1FF40165009238A1 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2B39E35D1FF40165009238A1 /* Launch Screen.storyboard */; };
1414
2B39E3621FF40251009238A1 /* JSHomeModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B39E3611FF40251009238A1 /* JSHomeModel.m */; };
15+
2B4D6180202A197600D7183A /* JSWKWebKitViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B4D617F202A197600D7183A /* JSWKWebKitViewController.m */; };
1516
2B65FC5F1FFBDAA0000322A1 /* intercept.html in Resources */ = {isa = PBXBuildFile; fileRef = 2B65FC5E1FFBDAA0000322A1 /* intercept.html */; };
1617
2B65FC631FFBDAE9000322A1 /* jscontext.html in Resources */ = {isa = PBXBuildFile; fileRef = 2B65FC621FFBDAE9000322A1 /* jscontext.html */; };
1718
2B65FC681FFBE624000322A1 /* JSUIInterceptViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B65FC671FFBE624000322A1 /* JSUIInterceptViewController.m */; };
@@ -29,6 +30,7 @@
2930
2B89FA461FEAB833001349A7 /* JSWKWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B89FA451FEAB833001349A7 /* JSWKWebViewController.m */; };
3031
2B89FA481FEABCE6001349A7 /* jsbridge.html in Resources */ = {isa = PBXBuildFile; fileRef = 2B89FA471FEABCE6001349A7 /* jsbridge.html */; };
3132
2BEB86331FEC0D090015F8E0 /* NSDictionary+Json.m in Sources */ = {isa = PBXBuildFile; fileRef = 2BEB86321FEC0D090015F8E0 /* NSDictionary+Json.m */; };
33+
B54D907C4B6FA64364AAF23E /* webkit.html in Resources */ = {isa = PBXBuildFile; fileRef = B54D9BF70A906DA37BB0711A /* webkit.html */; };
3234
/* End PBXBuildFile section */
3335

3436
/* Begin PBXFileReference section */
@@ -38,6 +40,8 @@
3840
2B39E35D1FF40165009238A1 /* Launch Screen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = "Launch Screen.storyboard"; sourceTree = "<group>"; };
3941
2B39E3601FF40251009238A1 /* JSHomeModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSHomeModel.h; sourceTree = "<group>"; };
4042
2B39E3611FF40251009238A1 /* JSHomeModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JSHomeModel.m; sourceTree = "<group>"; };
43+
2B4D617E202A197600D7183A /* JSWKWebKitViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSWKWebKitViewController.h; sourceTree = "<group>"; };
44+
2B4D617F202A197600D7183A /* JSWKWebKitViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JSWKWebKitViewController.m; sourceTree = "<group>"; };
4145
2B65FC5E1FFBDAA0000322A1 /* intercept.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = intercept.html; sourceTree = "<group>"; };
4246
2B65FC621FFBDAE9000322A1 /* jscontext.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = jscontext.html; sourceTree = "<group>"; };
4347
2B65FC661FFBE624000322A1 /* JSUIInterceptViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSUIInterceptViewController.h; sourceTree = "<group>"; };
@@ -72,6 +76,7 @@
7276
3D4F34A4DDAE73D3185F77E2 /* Pods-JsInterface.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JsInterface.release.xcconfig"; path = "Pods/Target Support Files/Pods-JsInterface/Pods-JsInterface.release.xcconfig"; sourceTree = "<group>"; };
7377
7F8F24BA95BC4A8D4709671E /* libPods-JsInterface.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-JsInterface.a"; sourceTree = BUILT_PRODUCTS_DIR; };
7478
A15400B2138DCEB842EA3E71 /* Pods-JsInterface.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JsInterface.debug.xcconfig"; path = "Pods/Target Support Files/Pods-JsInterface/Pods-JsInterface.debug.xcconfig"; sourceTree = "<group>"; };
79+
B54D9BF70A906DA37BB0711A /* webkit.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = webkit.html; sourceTree = "<group>"; };
7580
/* End PBXFileReference section */
7681

7782
/* Begin PBXFrameworksBuildPhase section */
@@ -108,6 +113,7 @@
108113
2B89FA471FEABCE6001349A7 /* jsbridge.html */,
109114
2B65FC5E1FFBDAA0000322A1 /* intercept.html */,
110115
2B65FC621FFBDAE9000322A1 /* jscontext.html */,
116+
B54D9BF70A906DA37BB0711A /* webkit.html */,
111117
);
112118
path = html;
113119
sourceTree = "<group>";
@@ -179,6 +185,8 @@
179185
2B69BA7B1FFD2F91008C28A5 /* JSWKInterceptViewController.m */,
180186
2B69BA7D1FFD2FA6008C28A5 /* JSWKBridgeViewController.h */,
181187
2B69BA7E1FFD2FA6008C28A5 /* JSWKBridgeViewController.m */,
188+
2B4D617E202A197600D7183A /* JSWKWebKitViewController.h */,
189+
2B4D617F202A197600D7183A /* JSWKWebKitViewController.m */,
182190
);
183191
path = Controller;
184192
sourceTree = "<group>";
@@ -360,6 +368,7 @@
360368
2B2A0045200875F200AC8FA8 /* JSHomeTableViewCell.xib in Resources */,
361369
2B65FC631FFBDAE9000322A1 /* jscontext.html in Resources */,
362370
2B89FA311FEAB1EC001349A7 /* Assets.xcassets in Resources */,
371+
B54D907C4B6FA64364AAF23E /* webkit.html in Resources */,
363372
);
364373
runOnlyForDeploymentPostprocessing = 0;
365374
};
@@ -430,6 +439,7 @@
430439
2B69BA7F1FFD2FA6008C28A5 /* JSWKBridgeViewController.m in Sources */,
431440
2BEB86331FEC0D090015F8E0 /* NSDictionary+Json.m in Sources */,
432441
2B89FA371FEAB1EC001349A7 /* main.m in Sources */,
442+
2B4D6180202A197600D7183A /* JSWKWebKitViewController.m in Sources */,
433443
2B65FC741FFBE840000322A1 /* NSURL+Params.m in Sources */,
434444
2B65FC6B1FFBE661000322A1 /* JSUIJsContextViewController.m in Sources */,
435445
2B89FA3F1FEAB7FD001349A7 /* JSHomeViewController.m in Sources */,

iOS/JsInterface/Class/Pages/Home/Controller/JSHomeViewController.m

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#import "JSWKInterceptViewController.h"
1717
#import "JSWKBridgeViewController.h"
18+
#import "JSWKWebKitViewController.h"
1819

1920
#import "JSHomeTableViewCell.h"
2021

@@ -45,13 +46,16 @@ - (void)viewDidLoad {
4546
JSHomeModel *wkwebviewIntercept = [[JSHomeModel alloc] initWithType:@"WKWebView" jsCallNative:@"拦截跳转" nativeCallJS:@"evaluateJavaScript:completionHandler" targetClass:[JSWKInterceptViewController class]];
4647

4748
JSHomeModel *uiwebviewJsContext = [[JSHomeModel alloc] initWithType:@"UIWebView" jsCallNative:@"JsContextExport" nativeCallJS:@"evaluateScript: 或 callWithArguments:" targetClass:[JSUIJsContextViewController class]];
49+
50+
JSHomeModel *wkWebKit = [[JSHomeModel alloc] initWithType:@"WKWebView" jsCallNative:@"window.webkit.messageHandlers" nativeCallJS:@"evaluateJavaScript:completionHandler" targetClass:[JSWKWebKitViewController class]];
4851

4952
JSHomeModel *uiJsbridge = [[JSHomeModel alloc] initWithType:@"UIWebView" jsCallNative:@"WebViewJavascriptBridge" nativeCallJS:@"callHandler" targetClass:[JSUIBridgeViewController class]];
5053
JSHomeModel *wkJsbridge = [[JSHomeModel alloc] initWithType:@"WKWebView" jsCallNative:@"WebViewJavascriptBridge" nativeCallJS:@"callHandler" targetClass:[JSWKBridgeViewController class]];
5154

5255
_modelArray = @[
5356
@[uiwebviewIntercept, wkwebviewIntercept],
5457
@[uiwebviewJsContext],
58+
@[wkWebKit],
5559
@[uiJsbridge, wkJsbridge],
5660
];
5761
}

iOS/JsInterface/Class/Pages/UIWebView/Controller/JSUIJsContextViewController.m

-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,5 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
4343
NSLog(@"异常信息:%@", exception);
4444
};
4545
self.jsContext[@"app"] = [[JSContextModel alloc] init];
46-
// [self.jsContext evaluateScript:<#(NSString *)#>]
4746
}
4847
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// JSWKWebKitViewController.h
3+
// JsInterface
4+
//
5+
// Created by 甘文鹏 on 2018/2/7.
6+
// Copyright © 2018年 ganwenpeng.com. All rights reserved.
7+
//
8+
9+
#import "JSWKWebViewController.h"
10+
11+
@interface JSWKWebKitViewController : JSWKWebViewController
12+
13+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// JSWKWebKitViewController.m
3+
// JsInterface
4+
//
5+
// Created by 甘文鹏 on 2018/2/7.
6+
// Copyright © 2018年 ganwenpeng.com. All rights reserved.
7+
//
8+
9+
#import "JSWKWebKitViewController.h"
10+
11+
#define FUNC_LOGIN @"login"
12+
#define FUNC_LOGOUT @"logout"
13+
14+
@interface JSWKWebKitViewController ()<WKScriptMessageHandler>
15+
16+
@end
17+
18+
@implementation JSWKWebKitViewController
19+
20+
- (void)viewDidLoad {
21+
[super viewDidLoad];
22+
23+
self.title = @"WKWebView - WebKti";
24+
WKUserContentController *confVc = self.webView.configuration.userContentController;
25+
[confVc addScriptMessageHandler:self name:FUNC_LOGIN];
26+
[confVc addScriptMessageHandler:self name:FUNC_LOGOUT];
27+
28+
// 加载测试用的HTML页面
29+
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"webkit" ofType:@"html"]];
30+
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
31+
}
32+
33+
- (void)btn1Click {
34+
[self.webView evaluateJavaScript:@"showResponse('点击了原生的按钮11111111111')" completionHandler:nil];
35+
}
36+
37+
- (void)btn2Click {
38+
[self.webView evaluateJavaScript:@"showResponse('点击了原生的按钮22222222222')" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
39+
if (error) {
40+
NSLog(@"%@", error);
41+
} else {
42+
NSLog(@"%@", response);
43+
}
44+
}];
45+
}
46+
47+
#pragma mark - WKScriptMessageHandler
48+
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
49+
if ([message.name isEqualToString:FUNC_LOGIN]) {
50+
if (![message.body isKindOfClass:[NSDictionary class]]) {
51+
return;
52+
}
53+
NSDictionary *data = message.body;
54+
NSString *account = data[@"account"];
55+
NSString *password = data[@"password"];
56+
57+
NSString *msg = [NSString stringWithFormat:@"执行登录操作,账号为:%@,密码为:%@", account, password];
58+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"原生弹窗" message:msg delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
59+
[alert show];
60+
return;
61+
}
62+
63+
if ([message.name isEqualToString:FUNC_LOGOUT]) {
64+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"原生弹窗" message:@"执行登出操作" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
65+
[alert show];
66+
return;
67+
}
68+
}
69+
@end

iOS/JsInterface/Class/Pages/WKWebView/Controller/JSWKWebViewController.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#import <UIKit/UIKit.h>
1010
#import <WebKit/WebKit.h>
1111

12-
@interface JSWKWebViewController : UIViewController<WKNavigationDelegate>
12+
@interface JSWKWebViewController : UIViewController<WKNavigationDelegate, WKUIDelegate>
1313
@property(nonatomic, strong) WKWebView *webView;
1414

1515
- (void)btn1Click;

iOS/JsInterface/Class/Pages/WKWebView/Controller/JSWKWebViewController.m

+2-3
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ - (void)setupUI {
6666
self.btn2 = btn2;
6767

6868
self.view.backgroundColor = [UIColor blackColor];
69-
self.webView = [[WKWebView alloc] init];
69+
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:[[WKWebViewConfiguration alloc] init]];
7070
self.webView.navigationDelegate = self;
71+
self.webView.UIDelegate = self;
7172
[self.view addSubview:self.webView];
7273
[self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
7374
make.left.bottom.right.equalTo(self.view);
@@ -81,6 +82,4 @@ - (void)btn1Click {
8182
- (void)btn2Click {
8283
}
8384

84-
- (void)btn3Click {
85-
}
8685
@end

iOS/JsInterface/Resource/html/jscontext.html

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050

5151
$(function () {
5252
$('#login').click(function () {
53-
window.location = 'app://login?account=13011112222&password=123456';
5453
app.login("13011112222", "123456");
5554
});
5655

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>JSBridge Test</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport"
7+
content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
8+
<script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.12.4/jquery.js"></script>
9+
10+
<style type="text/css">
11+
.btn {
12+
background-color: #aaa;
13+
height: 40px;
14+
display: flex;
15+
justify-content: center;
16+
align-items: center;
17+
color: #444444;
18+
text-decoration: none;
19+
margin-top: 10px;
20+
}
21+
22+
#response {
23+
background: #eeeeee;
24+
word-wrap: break-word;
25+
display: block;
26+
outline: 1px solid #ccc;
27+
padding: 5px;
28+
margin: 5px;
29+
}
30+
</style>
31+
</head>
32+
<body>
33+
<p style="color: red">嵌入页区域</p>
34+
<p>以webkit的方式实现</p>
35+
<button class="btn" id="login">执行登录操作</button>
36+
<button class="btn" id="logout">执行登出操作</button>
37+
38+
<pre id="response"></pre>
39+
40+
<script type="text/javascript">
41+
/**
42+
* 显示响应信息
43+
* @param response 响应信心
44+
*/
45+
function showResponse(response) {
46+
$('#response').text(response);
47+
}
48+
49+
/**
50+
* jQuery
51+
*/
52+
$(function () {
53+
$("#login").click(function () {
54+
window.webkit.messageHandlers.login.postMessage({
55+
'account': '13000000000',
56+
'password': '123456'
57+
});
58+
});
59+
60+
$('#logout').click(function () {
61+
window.webkit.messageHandlers.logout.postMessage(null);
62+
});
63+
});
64+
</script>
65+
</body>
66+
</html>

0 commit comments

Comments
 (0)