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 {
0 commit comments