Skip to content

Commit cd5b566

Browse files
committed
优化接口
1 parent ed79dc3 commit cd5b566

File tree

8 files changed

+130
-101
lines changed

8 files changed

+130
-101
lines changed

STMScriptMessageHandler.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
00E3CA6A22B4E678005C6806 /* WKWebView+STMScriptMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E3CA6922B4E678005C6806 /* WKWebView+STMScriptMessage.m */; };
1011
B8155B712116DAA900FD0739 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B8155B702116DAA900FD0739 /* AppDelegate.m */; };
1112
B8155B772116DAA900FD0739 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B8155B752116DAA900FD0739 /* Main.storyboard */; };
1213
B8155B792116DAAE00FD0739 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B8155B782116DAAE00FD0739 /* Assets.xcassets */; };
@@ -19,6 +20,8 @@
1920
/* End PBXBuildFile section */
2021

2122
/* Begin PBXFileReference section */
23+
00E3CA6822B4E678005C6806 /* WKWebView+STMScriptMessage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WKWebView+STMScriptMessage.h"; sourceTree = "<group>"; };
24+
00E3CA6922B4E678005C6806 /* WKWebView+STMScriptMessage.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "WKWebView+STMScriptMessage.m"; sourceTree = "<group>"; };
2225
B8155B6C2116DAA900FD0739 /* STMScriptMessageHandler.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = STMScriptMessageHandler.app; sourceTree = BUILT_PRODUCTS_DIR; };
2326
B8155B6F2116DAA900FD0739 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
2427
B8155B702116DAA900FD0739 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
@@ -82,6 +85,8 @@
8285
children = (
8386
B8155B882116DB9600FD0739 /* STMScriptMessageHandler.h */,
8487
B8155B862116DB9600FD0739 /* STMScriptMessageHandler.m */,
88+
00E3CA6822B4E678005C6806 /* WKWebView+STMScriptMessage.h */,
89+
00E3CA6922B4E678005C6806 /* WKWebView+STMScriptMessage.m */,
8590
);
8691
path = Source;
8792
sourceTree = "<group>";
@@ -173,6 +178,7 @@
173178
B8155B8E2116DB9700FD0739 /* STMScriptMessageHandler.m in Sources */,
174179
B8155B902116DB9700FD0739 /* STMViewController.m in Sources */,
175180
B8155B8F2116DB9700FD0739 /* STMWebViewController.m in Sources */,
181+
00E3CA6A22B4E678005C6806 /* WKWebView+STMScriptMessage.m in Sources */,
176182
B8155B7F2116DAAE00FD0739 /* main.m in Sources */,
177183
B8155B712116DAA900FD0739 /* AppDelegate.m in Sources */,
178184
);

STMScriptMessageHandler/Demo/STMViewController.m

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ @implementation STMViewController
2424

2525
- (void)viewDidLoad {
2626
[super viewDidLoad];
27+
[self prepareScriptMessageHandler];
2728
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
2829
[self.webView loadHTMLString:[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] baseURL:nil];
2930

@@ -36,31 +37,28 @@ - (void)viewDidLoad {
3637
}
3738

3839
- (void)prepareScriptMessageHandler {
39-
[super prepareScriptMessageHandler];
40-
4140
// Use `self.messageHandler` register a method for js, the js should call this use App.Bridge.callMethod...
42-
[self.messageHandler registerMethod:@"nslog" handler:^(id _Nonnull data, STMResponseCallback _Nullable responseCallback) {
41+
[self.webView.stm_defaultScriptMessageHandler registerMethod:@"nslog" handler:^(id _Nonnull data, STMResponseCallback _Nullable responseCallback) {
4342
NSLog(@"native receive js calling `nslog`: %@", data);
4443
responseCallback(@"native `nslog` done!");
4544
}];
4645

47-
[self.messageHandler registerMethod:@"testNativeMethod" handler:^(id _Nonnull data, STMResponseCallback _Nullable responseCallback) {
46+
[self.webView.stm_defaultScriptMessageHandler registerMethod:@"testNativeMethod" handler:^(id _Nonnull data, STMResponseCallback _Nullable responseCallback) {
4847
NSLog(@"native receive js calling `testNativeMethod`: %@", data);
4948
responseCallback(@(200));
5049
}];
5150

5251
// You can register yourself message handler.
5352
// register a message handler named `Page`, so the js should call your method that the message handler registered use App.Page.callMethod...
54-
self.page = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:@"Page" forWebView:self.webView];
55-
[self.webView stm_addScriptMessageHandler:self.page];
53+
self.page = [self.webView stm_addScriptMessageHandlerUseName:@"Page"];
5654

5755
[self.page registerMethod:@"setButtons" handler:^(id data, STMResponseCallback responseCallback) {
5856
[self setupRightBarButtonItems:data callback:responseCallback];
5957
}];
6058
}
6159

6260
- (void)onClick {
63-
[self.messageHandler callMethod:@"log" parameters:@{@"title": @"js method"} responseHandler:^(id _Nonnull responseData) {
61+
[self.webView.stm_defaultScriptMessageHandler callMethod:@"log" parameters:@{@"title": @"js method"} responseHandler:^(id _Nonnull responseData) {
6462
NSLog(@"native got js response for `log`: %@", responseData);
6563
}];
6664
}

STMScriptMessageHandler/Demo/STMWebViewController.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@
66
//
77

88
@import UIKit;
9-
#import "STMScriptMessageHandler.h"
9+
#import "WKWebView+STMScriptMessage.h"
1010

1111
NS_ASSUME_NONNULL_BEGIN
1212

1313
@interface STMWebViewController : UIViewController
1414

1515
@property (nonatomic, strong, readonly) WKWebView *webView;
1616
@property (nonatomic, strong) UIProgressView *progressView;
17-
@property (nullable, nonatomic, strong, readonly) STMScriptMessageHandler *messageHandler;
18-
19-
/// register your message handler in this method
20-
- (void)prepareScriptMessageHandler NS_REQUIRES_SUPER;
21-
- (NSArray<__kindof STMScriptMessageHandler *> *)registeredMessageHandlers;
2217

2318
@end
2419

STMScriptMessageHandler/Demo/STMWebViewController.m

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,9 @@ - (void)viewDidLayoutSubviews {
5050
CGRectGetWidth(self.navigationController.navigationBar.frame), 2);
5151
}
5252

53-
- (void)prepareScriptMessageHandler {
54-
STMScriptMessageHandler *messageHandler = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:@"Bridge" forWebView:self.webView];
55-
_messageHandler = messageHandler;
56-
[self.webView stm_addScriptMessageHandler:messageHandler];
57-
}
58-
5953
#pragma mark - Private Methods
6054

6155
- (void)_initial {
62-
[self prepareScriptMessageHandler];
6356
[self _addObserver];
6457
}
6558

@@ -156,8 +149,4 @@ - (UIProgressView *)progressView {
156149
return _progressView;
157150
}
158151

159-
- (NSArray<STMScriptMessageHandler *> *)registeredMessageHandlers {
160-
return [self.webView stm_registeredMessageHandlers];
161-
}
162-
163152
@end

STMScriptMessageHandler/Source/STMScriptMessageHandler.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef void (^STMHandler)(id data, STMResponseCallback _Nullable responseCallba
1717

1818
@property (nonatomic, copy, readonly) NSString *handlerName;
1919

20+
+ (instancetype)new NS_UNAVAILABLE;
2021
- (instancetype)init NS_UNAVAILABLE;
2122
- (instancetype)initWithScriptMessageHandlerName:(NSString *)handlerName forWebView:(__weak WKWebView *)webView;
2223

@@ -27,11 +28,5 @@ typedef void (^STMHandler)(id data, STMResponseCallback _Nullable responseCallba
2728

2829
@end
2930

30-
@interface WKWebView (STMScriptMessage)
31-
32-
- (void)stm_addScriptMessageHandler:(__kindof STMScriptMessageHandler *)msgHandler;
33-
- (NSArray<STMScriptMessageHandler *> *)stm_registeredMessageHandlers;
34-
35-
@end
3631

3732
NS_ASSUME_NONNULL_END

STMScriptMessageHandler/Source/STMScriptMessageHandler.m

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
//
77

88
#import "STMScriptMessageHandler.h"
9-
#import <objc/runtime.h>
109

1110
#define STM_JS_FUNC(x, ...) [NSString stringWithFormat:@#x ,##__VA_ARGS__]
1211
#define WEAK_SELF __weak typeof(self) __weak_self__ = self
1312
#define STRONG_SELF __strong typeof(__weak_self__) self = __weak_self__
14-
void _STMObjcSwizzMethod(Class aClass, SEL originSelector, SEL swizzSelector);
1513

1614
static NSString * const kSTMApp = @"App";
1715
static NSString * const kSTMNativeCallback = @"nativeCallback";
@@ -212,72 +210,3 @@ - (NSMutableDictionary *)jsResponseHandlers {
212210
}
213211

214212
@end
215-
216-
#pragma mark -
217-
218-
static char * const kSTMWebViewScriptMessageHandlersKey = "kSTMWebViewScriptMessageHandlersKey";
219-
220-
@implementation WKWebView (STMScriptMessage)
221-
222-
+ (void)load {
223-
static dispatch_once_t onceToken;
224-
dispatch_once(&onceToken, ^{
225-
SEL systemSel = NSSelectorFromString(@"dealloc");
226-
SEL swizzSel = @selector(stm_dealloc);
227-
_STMObjcSwizzMethod(self, systemSel, swizzSel);
228-
});
229-
}
230-
231-
- (void)stm_dealloc {
232-
for (STMScriptMessageHandler *messageHandler in [self stm_scriptMessageHandlers]) {
233-
[self _stm_removeScriptMessageHandler:messageHandler];
234-
}
235-
}
236-
237-
- (void)stm_addScriptMessageHandler:(__kindof STMScriptMessageHandler *)msgHandler {
238-
[[self stm_scriptMessageHandlers] addObject:msgHandler];
239-
[self _stm_addScriptMessageHandler:msgHandler];
240-
}
241-
242-
- (NSArray<STMScriptMessageHandler *> *)stm_registeredMessageHandlers {
243-
return [[self stm_scriptMessageHandlers] copy];
244-
}
245-
246-
- (void)_stm_addScriptMessageHandler:(__kindof STMScriptMessageHandler *)messageHandler {
247-
WKUserContentController *userContentController = self.configuration.userContentController;
248-
[userContentController removeScriptMessageHandlerForName:messageHandler.handlerName];
249-
[userContentController addScriptMessageHandler:messageHandler name:messageHandler.handlerName];
250-
}
251-
252-
- (void)_stm_removeScriptMessageHandler:(__kindof STMScriptMessageHandler *)messageHandler {
253-
WKUserContentController *userContentController = self.configuration.userContentController;
254-
[userContentController removeScriptMessageHandlerForName:messageHandler.handlerName];
255-
}
256-
257-
- (NSMutableArray<STMScriptMessageHandler *> *)stm_scriptMessageHandlers {
258-
NSMutableArray<STMScriptMessageHandler *> *array = objc_getAssociatedObject(self, kSTMWebViewScriptMessageHandlersKey);
259-
if (!array) {
260-
array = [NSMutableArray array];
261-
objc_setAssociatedObject(self, kSTMWebViewScriptMessageHandlersKey, array, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
262-
}
263-
return array;
264-
}
265-
266-
@end
267-
268-
void _STMObjcSwizzMethod(Class aClass, SEL originSelector, SEL swizzSelector) {
269-
Method systemMethod = class_getInstanceMethod(aClass, originSelector);
270-
Method swizzMethod = class_getInstanceMethod(aClass, swizzSelector);
271-
BOOL isAdd = class_addMethod(aClass,
272-
originSelector,
273-
method_getImplementation(swizzMethod),
274-
method_getTypeEncoding(swizzMethod));
275-
if (isAdd) {
276-
class_replaceMethod(aClass,
277-
swizzSelector,
278-
method_getImplementation(systemMethod),
279-
method_getTypeEncoding(systemMethod));
280-
} else {
281-
method_exchangeImplementations(systemMethod, swizzMethod);
282-
}
283-
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// WKWebView+STMScriptMessage.h
3+
// STMScriptMessageHandler
4+
//
5+
// Created by DouKing on 2019/6/15.
6+
//
7+
8+
#import "STMScriptMessageHandler.h"
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface WKWebView (STMScriptMessage)
13+
14+
@property (nullable, nonatomic, strong, readonly) STMScriptMessageHandler *stm_defaultScriptMessageHandler;
15+
@property (nullable, nonatomic, strong, readonly) NSArray<STMScriptMessageHandler *> *stm_registeredMessageHandlers;
16+
17+
- (nullable STMScriptMessageHandler *)stm_addScriptMessageHandlerUseName:(NSString *)handlerName;
18+
- (void)stm_addScriptMessageHandler:(__kindof STMScriptMessageHandler *)msgHandler;
19+
20+
@end
21+
22+
NS_ASSUME_NONNULL_END
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// WKWebView+STMScriptMessage.m
3+
// STMScriptMessageHandler
4+
//
5+
// Created by DouKing on 2019/6/15.
6+
//
7+
8+
#import "WKWebView+STMScriptMessage.h"
9+
#import <objc/runtime.h>
10+
11+
static char * const kSTMWebViewScriptMessageHandlersKey = "kSTMWebViewScriptMessageHandlersKey";
12+
static NSString * const kSTMWebViewScriptMessageHandlerDefaultName = @"Bridge";
13+
void _STMObjcSwizzMethod(Class aClass, SEL originSelector, SEL swizzSelector);
14+
15+
@implementation WKWebView (STMScriptMessage)
16+
17+
+ (void)load {
18+
static dispatch_once_t onceToken;
19+
dispatch_once(&onceToken, ^{
20+
SEL systemSel = NSSelectorFromString(@"dealloc");
21+
SEL swizzSel = @selector(stm_dealloc);
22+
_STMObjcSwizzMethod(self, systemSel, swizzSel);
23+
});
24+
}
25+
26+
- (void)stm_dealloc {
27+
for (STMScriptMessageHandler *messageHandler in [self stm_scriptMessageHandlers]) {
28+
[self _stm_removeScriptMessageHandler:messageHandler];
29+
}
30+
}
31+
32+
- (STMScriptMessageHandler *)stm_addScriptMessageHandlerUseName:(NSString *)handlerName {
33+
if (!handlerName || !handlerName.length) { return nil; }
34+
STMScriptMessageHandler *msgHandler = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:handlerName forWebView:self];
35+
[self stm_addScriptMessageHandler:msgHandler];
36+
return msgHandler;
37+
}
38+
39+
- (void)stm_addScriptMessageHandler:(__kindof STMScriptMessageHandler *)msgHandler {
40+
if (!msgHandler) { return; }
41+
[[self stm_scriptMessageHandlers] addObject:msgHandler];
42+
[self _stm_addScriptMessageHandler:msgHandler];
43+
}
44+
45+
- (void)_stm_addScriptMessageHandler:(__kindof STMScriptMessageHandler *)messageHandler {
46+
WKUserContentController *userContentController = self.configuration.userContentController;
47+
[userContentController removeScriptMessageHandlerForName:messageHandler.handlerName];
48+
[userContentController addScriptMessageHandler:messageHandler name:messageHandler.handlerName];
49+
}
50+
51+
- (void)_stm_removeScriptMessageHandler:(__kindof STMScriptMessageHandler *)messageHandler {
52+
WKUserContentController *userContentController = self.configuration.userContentController;
53+
[userContentController removeScriptMessageHandlerForName:messageHandler.handlerName];
54+
}
55+
56+
- (STMScriptMessageHandler *)stm_defaultScriptMessageHandler {
57+
STMScriptMessageHandler *msgHandler = objc_getAssociatedObject(self, _cmd);
58+
if (!msgHandler) {
59+
msgHandler = [self stm_addScriptMessageHandlerUseName:kSTMWebViewScriptMessageHandlerDefaultName];
60+
objc_setAssociatedObject(self, _cmd, msgHandler, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
61+
}
62+
return msgHandler;
63+
}
64+
65+
- (NSArray<STMScriptMessageHandler *> *)stm_registeredMessageHandlers {
66+
return [[self stm_scriptMessageHandlers] copy];
67+
}
68+
69+
- (NSMutableArray<STMScriptMessageHandler *> *)stm_scriptMessageHandlers {
70+
NSMutableArray<STMScriptMessageHandler *> *array = objc_getAssociatedObject(self, kSTMWebViewScriptMessageHandlersKey);
71+
if (!array) {
72+
array = [NSMutableArray array];
73+
objc_setAssociatedObject(self, kSTMWebViewScriptMessageHandlersKey, array, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
74+
}
75+
return array;
76+
}
77+
78+
@end
79+
80+
void _STMObjcSwizzMethod(Class aClass, SEL originSelector, SEL swizzSelector) {
81+
Method systemMethod = class_getInstanceMethod(aClass, originSelector);
82+
Method swizzMethod = class_getInstanceMethod(aClass, swizzSelector);
83+
BOOL isAdd = class_addMethod(aClass,
84+
originSelector,
85+
method_getImplementation(swizzMethod),
86+
method_getTypeEncoding(swizzMethod));
87+
if (isAdd) {
88+
class_replaceMethod(aClass,
89+
swizzSelector,
90+
method_getImplementation(systemMethod),
91+
method_getTypeEncoding(systemMethod));
92+
} else {
93+
method_exchangeImplementations(systemMethod, swizzMethod);
94+
}
95+
}

0 commit comments

Comments
 (0)