Skip to content

Commit 7d8e0d9

Browse files
新增UIWebVie‘
git commit -m 新增UIWebVie
1 parent 857340e commit 7d8e0d9

11 files changed

Lines changed: 139 additions & 37 deletions

File tree

‎.DS_Store‎

0 Bytes
Binary file not shown.

‎Hybrid-framework/.DS_Store‎

0 Bytes
Binary file not shown.

‎Hybrid-framework/Hybrid-framework.xcodeproj/project.pbxproj‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
0A9E4693225B2F07003F6574 /* SHRMPushDemoPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A9E4692225B2F07003F6574 /* SHRMPushDemoPlugin.m */; };
1212
0A9E46A5225E0330003F6574 /* SHRMFetchPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A9E46A3225E0330003F6574 /* SHRMFetchPlugin.m */; };
1313
0A9E46A8225E0342003F6574 /* test.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A9E46A7225E0342003F6574 /* test.m */; };
14+
0ABC13D4226323DA009E19E6 /* SHRMUIWebViewJavaScriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ABC13D3226323DA009E19E6 /* SHRMUIWebViewJavaScriptBridge.m */; };
1415
0AC786F02255AB3C00CD5054 /* SHRMWebViewCookieMgr.m in Sources */ = {isa = PBXBuildFile; fileRef = 0AC786EF2255AB3C00CD5054 /* SHRMWebViewCookieMgr.m */; };
1516
832F7C8821B822510076D164 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 832F7C8721B822510076D164 /* AppDelegate.m */; };
1617
832F7C8B21B822510076D164 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 832F7C8A21B822510076D164 /* ViewController.m */; };
@@ -38,6 +39,8 @@
3839
0A9E46A4225E0330003F6574 /* SHRMFetchPlugin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SHRMFetchPlugin.h; sourceTree = "<group>"; };
3940
0A9E46A6225E0342003F6574 /* test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = test.h; sourceTree = "<group>"; };
4041
0A9E46A7225E0342003F6574 /* test.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = test.m; sourceTree = "<group>"; };
42+
0ABC13D2226323DA009E19E6 /* SHRMUIWebViewJavaScriptBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SHRMUIWebViewJavaScriptBridge.h; sourceTree = "<group>"; };
43+
0ABC13D3226323DA009E19E6 /* SHRMUIWebViewJavaScriptBridge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SHRMUIWebViewJavaScriptBridge.m; sourceTree = "<group>"; };
4144
0AC786EE2255AB3C00CD5054 /* SHRMWebViewCookieMgr.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SHRMWebViewCookieMgr.h; sourceTree = "<group>"; };
4245
0AC786EF2255AB3C00CD5054 /* SHRMWebViewCookieMgr.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SHRMWebViewCookieMgr.m; sourceTree = "<group>"; };
4346
832F7C8321B822510076D164 /* Hybrid-framework.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Hybrid-framework.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -143,6 +146,8 @@
143146
832F7E7B21BB72C50076D164 /* SHRMWebPluginAnnotation.m */,
144147
0AC786EE2255AB3C00CD5054 /* SHRMWebViewCookieMgr.h */,
145148
0AC786EF2255AB3C00CD5054 /* SHRMWebViewCookieMgr.m */,
149+
0ABC13D2226323DA009E19E6 /* SHRMUIWebViewJavaScriptBridge.h */,
150+
0ABC13D3226323DA009E19E6 /* SHRMUIWebViewJavaScriptBridge.m */,
146151
);
147152
path = JSBridge;
148153
sourceTree = "<group>";
@@ -250,6 +255,7 @@
250255
832F7E5921BA38F70076D164 /* SHRMWebViewDelegate.m in Sources */,
251256
832F7C8821B822510076D164 /* AppDelegate.m in Sources */,
252257
832F7E8B21BBC12A0076D164 /* SHRMDevicePlugin.m in Sources */,
258+
0ABC13D4226323DA009E19E6 /* SHRMUIWebViewJavaScriptBridge.m in Sources */,
253259
);
254260
runOnlyForDeploymentPostprocessing = 0;
255261
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// SHRMUIWebViewJavaScriptBridge.h
3+
// Hybrid-framework
4+
//
5+
// Created by Kevin on 2019/4/14.
6+
// Copyright © 2019 王凯. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <WebKit/WebKit.h>
11+
12+
#import "SHRMWebViewProtocol.h"
13+
14+
NS_ASSUME_NONNULL_BEGIN
15+
16+
@interface SHRMUIWebViewJavaScriptBridge : NSObject <UIWebViewDelegate, SHRMWebViewProtocol>
17+
+ (instancetype)bindBridgeWithWebView:(UIWebView *)webView;
18+
/**
19+
webView容器
20+
*/
21+
@property (nonatomic, weak) id webViewDelegate;
22+
@end
23+
24+
NS_ASSUME_NONNULL_END
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// SHRMUIWebViewJavaScriptBridge.m
3+
// Hybrid-framework
4+
//
5+
// Created by Kevin on 2019/4/14.
6+
// Copyright © 2019 王凯. All rights reserved.
7+
//
8+
9+
#import "SHRMUIWebViewJavaScriptBridge.h"
10+
#import "SHRMWebViewHandleFactory.h"
11+
12+
@implementation SHRMUIWebViewJavaScriptBridge {
13+
__weak UIWebView *_webView;
14+
SHRMWebViewHandleFactory *_webViewHandleFactory;
15+
}
16+
17+
@synthesize rootViewController = _rootViewController;
18+
19+
+ (instancetype)bindBridgeWithWebView:(UIWebView *)webView; {
20+
return [[self alloc] initWithWebView:webView];
21+
}
22+
23+
- (instancetype)initWithWebView:(UIWebView *)webView {
24+
if (self = [super init]) {
25+
_webView = webView;
26+
_webView.delegate = self;
27+
_webViewHandleFactory = [[SHRMWebViewHandleFactory alloc] init];
28+
_webViewHandleFactory.webViewBridge = self;
29+
}
30+
return self;
31+
}
32+
33+
- (void)setWebViewDelegate:(NSObject<UIWebViewDelegate> *)webViewDelegate {
34+
_webViewDelegate = webViewDelegate;
35+
if ([webViewDelegate isKindOfClass:[UIViewController class]]) {
36+
self.rootViewController = (UIViewController *)webViewDelegate;
37+
}
38+
}
39+
40+
#pragma mark - SHRMWebViewProtocol
41+
42+
- (void)sendPluginResult:(NSString *)result callbackId:(NSString*)callbackId {
43+
NSString *jsStr = [NSString stringWithFormat:@"fetchComplete('(%@)','%@')",callbackId,result];
44+
[_webView stringByEvaluatingJavaScriptFromString:jsStr];
45+
}
46+
47+
- (void)runInBackground:(void (^)(void))block {
48+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
49+
}
50+
51+
#pragma mark - uiwebview delegate
52+
53+
- (void)webViewDidStartLoad:(UIWebView *)webView {
54+
55+
}
56+
- (void)webViewDidFinishLoad:(UIWebView *)webView {
57+
58+
}
59+
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
60+
61+
}
62+
63+
@end

‎Hybrid-framework/Hybrid-framework/JSBridge/SHRMWebViewEngine.h‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ NS_ASSUME_NONNULL_BEGIN
2121
*/
2222
@property (nonatomic, weak, readonly) WKWebView *webView;
2323

24+
25+
/**
26+
webView容器
27+
*/
2428
@property (nonatomic, weak) id webViewDelegate;
2529

2630
/**
@@ -39,10 +43,18 @@ NS_ASSUME_NONNULL_BEGIN
3943
/**
4044
webView绑定 bridge初始化
4145
42-
@param webView WKWebView
46+
@param webView WKWebView & UIWebView
4347
@return bridge
4448
*/
45-
+ (instancetype)bindBridgeWithWebView:(WKWebView *)webView;
49+
+ (instancetype)bindBridgeWithWebView:(id)webView;
50+
51+
/**
52+
插件预加载接口 onload 为 1 表示提前初始化此插件对象 否则在调用时初始化
53+
54+
@param pluginName name
55+
@param onload onload
56+
*/
57+
- (void)registerStartupPluginName:(NSString *)pluginName onload:(NSNumber *)onload;
4658
@end
4759

4860
NS_ASSUME_NONNULL_END

‎Hybrid-framework/Hybrid-framework/JSBridge/SHRMWebViewEngine.m‎

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,44 @@
99
#import "SHRMWebViewEngine.h"
1010
#import "SHRMWebViewDelegate.h"
1111
#import "SHRMWebViewHandleFactory.h"
12+
#import "SHRMUIWebViewJavaScriptBridge.h"
1213

1314
@interface SHRMWebViewEngine ()
1415
@property (nonatomic, strong) SHRMWebViewDelegate *WKWebViewDelegate;
1516
@property (nonatomic, strong) SHRMWebViewHandleFactory *webViewhandleFactory;
1617
@property (nonatomic, strong) SHRMWebPluginAnnotation *webPluginAnnotation;
1718
@property (nonatomic, strong) NSMutableDictionary *pluginObject;
1819
@property (nonatomic, weak, readwrite) WKWebView *webView;
19-
2020
@end
2121

2222
@implementation SHRMWebViewEngine
2323

2424
@synthesize rootViewController = _rootViewController;
2525

26-
- (instancetype)init {
27-
if (self = [super init]) {
28-
_webViewhandleFactory = [[SHRMWebViewHandleFactory alloc] initWithWebViewEngine:self];
29-
_WKWebViewDelegate = [[SHRMWebViewDelegate alloc] initWithWebViewEngine:self];
30-
_webPluginAnnotation = [[SHRMWebPluginAnnotation alloc] initWithWebViewEngine:self];
31-
_pluginObject = [NSMutableDictionary dictionary];
32-
[self loadStartupPlugin];
33-
}
34-
return self;
35-
}
36-
3726
#pragma mark - bridge
3827

39-
+ (instancetype)bindBridgeWithWebView:(WKWebView *)webView {
28+
+ (instancetype)bindBridgeWithWebView:(id)webView {
4029
return [self bridge:webView];
4130
}
4231

4332
+ (instancetype)bridge:(id)webView {
4433
if ([webView isKindOfClass:[WKWebView class]]) {
4534
SHRMWebViewEngine *bridge = [[SHRMWebViewEngine alloc] init];
46-
[bridge setupInstance:webView];
35+
[bridge configWKWebView:webView];
4736
[bridge addUserScript:webView];
37+
[bridge setupInstance];
38+
[bridge loadStartupPlugin];
4839
return bridge;
4940
}
41+
42+
if ([webView isKindOfClass:[UIWebView class]]) {
43+
return (SHRMWebViewEngine *)[SHRMUIWebViewJavaScriptBridge bindBridgeWithWebView:webView];
44+
}
5045
[NSException raise:@"BadWebViewType" format:@"Unknown web view type."];
5146
return nil;
5247
}
5348

54-
- (void)setupInstance:(WKWebView *)webView {
49+
- (void)configWKWebView:(WKWebView *)webView {
5550
_webView = webView;
5651
_webView.navigationDelegate = _WKWebViewDelegate;
5752
_webView.configuration.userContentController = [[WKUserContentController alloc] init];
@@ -67,6 +62,14 @@ - (void)addUserScript:(WKWebView *)webView {
6762
[webView.configuration.userContentController addUserScript:jsscript];
6863
}
6964

65+
- (void)setupInstance{
66+
_webViewhandleFactory = [[SHRMWebViewHandleFactory alloc] initWithWebViewEngine:self];
67+
_webViewhandleFactory.webViewBridge = self;
68+
_WKWebViewDelegate = [[SHRMWebViewDelegate alloc] initWithWebViewEngine:self];
69+
_webPluginAnnotation = [[SHRMWebPluginAnnotation alloc] initWithWebViewEngine:self];
70+
_pluginObject = [NSMutableDictionary dictionary];
71+
}
72+
7073
#pragma mark - WKScriptMessageHandler
7174

7275
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
@@ -79,21 +82,13 @@ - (void)userContentController:(WKUserContentController *)userContentController d
7982

8083
- (void)sendPluginResult:(NSString *)result callbackId:(NSString*)callbackId {
8184
NSString *jsStr = [NSString stringWithFormat:@"fetchComplete('(%@)','%@')",callbackId,result];
82-
[self.webView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
83-
NSLog(@"%@----%@",result, error);
84-
}];
85+
[self.webView evaluateJavaScript:jsStr completionHandler:nil];
8586
}
8687

8788
- (void)runInBackground:(void (^)(void))block {
8889
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
8990
}
9091

91-
- (void)registerStartupPluginName:(NSString *)pluginName onload:(NSNumber *)onload {
92-
if ([onload boolValue]) {
93-
[self getCommandInstance:pluginName];
94-
}
95-
}
96-
9792
#pragma mark - plugin handle
9893

9994
- (void)loadStartupPlugin {
@@ -113,15 +108,19 @@ - (id)getCommandInstance:(NSString*)pluginName {
113108
return obj;
114109
}
115110

116-
#pragma setter & getter
117-
118111
- (void)setWebViewDelegate:(NSObject<UIWebViewDelegate> *)webViewDelegate {
119112
_webViewDelegate = webViewDelegate;
120113
if ([webViewDelegate isKindOfClass:[UIViewController class]]) {
121114
self.rootViewController = (UIViewController *)webViewDelegate;
122115
}
123116
}
124117

118+
- (void)registerStartupPluginName:(NSString *)pluginName onload:(NSNumber *)onload {
119+
if ([onload boolValue]) {
120+
[self getCommandInstance:pluginName];
121+
}
122+
}
123+
125124
#pragma mark - dealloc
126125

127126
- (void)dealloc {

‎Hybrid-framework/Hybrid-framework/JSBridge/SHRMWebViewHandleFactory.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
NS_ASSUME_NONNULL_BEGIN
1212

1313
@class SHRMWebViewEngine;
14+
@protocol SHRMWebViewProtocol;
1415

1516
@interface SHRMWebViewHandleFactory : NSObject
17+
@property (nonatomic, weak) id<SHRMWebViewProtocol> webViewBridge;
1618
- (void)handleMsgCommand:(NSArray *)arguments;
1719
- (instancetype)initWithWebViewEngine:(SHRMWebViewEngine *)webViewEngine;
1820
@end

‎Hybrid-framework/Hybrid-framework/JSBridge/SHRMWebViewHandleFactory.m‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ - (void)handleCommand:(NSArray *)commandArray {
3636

3737
- (void)executePending:(NSArray *)command {
3838
SHRMMsgCommand *msgCommand = [SHRMMsgCommand commandFromJson:command];
39-
msgCommand.delegate = _webViewEngine;
39+
msgCommand.delegate = _webViewBridge;
4040
if (![self execute:msgCommand]) {
4141
NSLog(@"%@.%@ execute fail",msgCommand.className, msgCommand.methodName);
4242
}
@@ -68,4 +68,8 @@ - (BOOL)execute:(SHRMMsgCommand *)command {
6868
return retVal;
6969
}
7070

71+
- (void)dealloc {
72+
_webViewEngine = nil;
73+
}
74+
7175
@end

0 commit comments

Comments
 (0)