Skip to content

Commit d914b4e

Browse files
author
花菜
committed
逻辑优化
1 parent 44408e3 commit d914b4e

File tree

9 files changed

+60
-36
lines changed

9 files changed

+60
-36
lines changed

Example/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
99
:path: "../"
1010

1111
SPEC CHECKSUMS:
12-
SFWebViewBridge: 7cb5c365921045dda3a2aadad01eec87a170e752
12+
SFWebViewBridge: 0f5c6141624e2f7742ccf97e97bb1a147d146441
1313

1414
PODFILE CHECKSUM: 9067ff3e23bf55bd5aa801dc88fd1faf368acb66
1515

Example/Pods/Local Podspecs/SFWebViewBridge.podspec.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SFWebViewBridge.xcodeproj/project.pbxproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@
263263
isa = PBXProject;
264264
attributes = {
265265
CLASSPREFIX = SF;
266-
LastUpgradeCheck = 0720;
266+
LastUpgradeCheck = 0830;
267267
ORGANIZATIONNAME = chriscaixx;
268268
TargetAttributes = {
269269
6003F5AD195388D20070C39A = {
@@ -477,14 +477,19 @@
477477
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
478478
CLANG_WARN_EMPTY_BODY = YES;
479479
CLANG_WARN_ENUM_CONVERSION = YES;
480+
CLANG_WARN_INFINITE_RECURSION = YES;
480481
CLANG_WARN_INT_CONVERSION = YES;
481482
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
483+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
484+
CLANG_WARN_UNREACHABLE_CODE = YES;
482485
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
483486
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
484487
COPY_PHASE_STRIP = NO;
488+
ENABLE_STRICT_OBJC_MSGSEND = YES;
485489
ENABLE_TESTABILITY = YES;
486490
GCC_C_LANGUAGE_STANDARD = gnu99;
487491
GCC_DYNAMIC_NO_PIC = NO;
492+
GCC_NO_COMMON_BLOCKS = YES;
488493
GCC_OPTIMIZATION_LEVEL = 0;
489494
GCC_PREPROCESSOR_DEFINITIONS = (
490495
"DEBUG=1",
@@ -517,13 +522,18 @@
517522
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
518523
CLANG_WARN_EMPTY_BODY = YES;
519524
CLANG_WARN_ENUM_CONVERSION = YES;
525+
CLANG_WARN_INFINITE_RECURSION = YES;
520526
CLANG_WARN_INT_CONVERSION = YES;
521527
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
528+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
529+
CLANG_WARN_UNREACHABLE_CODE = YES;
522530
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
523531
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
524532
COPY_PHASE_STRIP = YES;
525533
ENABLE_NS_ASSERTIONS = NO;
534+
ENABLE_STRICT_OBJC_MSGSEND = YES;
526535
GCC_C_LANGUAGE_STANDARD = gnu99;
536+
GCC_NO_COMMON_BLOCKS = YES;
527537
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
528538
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
529539
GCC_WARN_UNDECLARED_SELECTOR = YES;

Example/SFWebViewBridge.xcodeproj/xcshareddata/xcschemes/SFWebViewBridge-Example.xcscheme

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0830"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -52,7 +52,7 @@
5252
</AdditionalOptions>
5353
</TestAction>
5454
<LaunchAction
55-
buildConfiguration = "Debug"
55+
buildConfiguration = "Release"
5656
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5757
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
5858
launchStyle = "0"

Example/SFWebViewBridge/Resource/local.html

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@
1313
function test2(data){
1414
alert(data)
1515
};
16-
function test3(){
17-
18-
alert("调用原生")
19-
20-
var json = '{ "name": "cxh", "sex": "man" }';
21-
22-
var str2 = { "name": "cxh", "sex": "man" };
23-
24-
this.printHelloWorld(str2)
25-
};
2616

2717
</script>
2818
<style type="text/css">

Example/SFWebViewBridge/SFViewController.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//
88

99
#import "SFViewController.h"
10-
#import "SFWebViewBridge.h"
10+
#import <SFWebViewBridge/SFWebViewBridge.h>
1111
#import "SFModel.h"
12-
@interface SFViewController ()<SFWebViewBridgeDelegate>
12+
@interface SFViewController ()<UIWebViewDelegate>
1313
@property (nonatomic, strong) UIWebView * webView;
1414
@property (nonatomic, strong) SFWebViewBridge * bridge;
1515
@end
@@ -23,6 +23,9 @@ - (void)viewDidLoad
2323

2424
NSString *path = [[NSBundle mainBundle] pathForResource:@"local.html" ofType:nil];
2525
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
26+
27+
_bridge = [SFWebViewBridge bridgeWithWebView:self.webView delegate:self];
28+
2629
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];
2730

2831

@@ -95,10 +98,9 @@ - (UIWebView *)webView
9598
if (!_webView)
9699
{
97100
_webView = [[UIWebView alloc] init];
98-
_webView.delegate = self;
99101
[self.view addSubview:_webView];
100102
_webView.frame = self.view.bounds;
101-
_bridge = [SFWebViewBridge bridgeWithWebView:_webView delegate:self];
103+
102104
}
103105
return _webView;
104106
}

SFWebViewBridge/Classes/SFWebViewBridge.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,30 @@
77
//
88

99
#import <UIKit/UIKit.h>
10+
@interface SFWebViewBridge : NSObject
1011

11-
@class SFWebViewBridge;
12-
@protocol SFWebViewBridgeDelegate <UIWebViewDelegate>
12+
/**
13+
快速创建桥
1314
14-
@end
15+
@param webView 需要建立桥接的 webView
16+
@param delegate 桥接代理一般为webView所属控制器
17+
@return bridge
18+
*/
19+
+ (instancetype)bridgeWithWebView:(UIWebView *)webView delegate:(id<UIWebViewDelegate>)delegate;
1520

16-
@interface SFWebViewBridge : NSObject
17-
@property (nonatomic, weak) id<SFWebViewBridgeDelegate> delegate;
18-
+ (instancetype)bridgeWithWebView:(UIWebView *)webView delegate:(id)delegate;
21+
/**
22+
调用不带参数的 JS 方法
23+
24+
@param handlerName 方法名
25+
*/
1926
- (void)callHandler:(NSString*)handlerName;
27+
28+
/**
29+
调用带参数的 JS 方法
30+
31+
@param handlerName 方法名
32+
@param parameter 参数,建议使用 NSDictionary类型
33+
*/
2034
- (void)callHandler:(NSString*)handlerName parameter:(id)parameter;
2135

2236
/**

SFWebViewBridge/Classes/SFWebViewBridge.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@
88

99
#import "SFWebViewBridge.h"
1010
#import <JavaScriptCore/JavaScriptCore.h>
11-
1211
@interface SFWebViewBridge ()<UIWebViewDelegate>
12+
@property (nonatomic, weak) id<UIWebViewDelegate> delegate;
1313
@property (nonatomic, strong) JSContext * context;
14-
@property (nonatomic, strong) UIWebView * webView;
1514
@property (nonatomic, strong) NSMutableDictionary * messageHandlers;
1615
@end
1716
@implementation SFWebViewBridge
1817
+ (instancetype)bridgeWithWebView:(UIWebView *)webView delegate:(id)delegate;
1918
{
2019
SFWebViewBridge * bridge = [[SFWebViewBridge alloc] init];
2120
webView.delegate = bridge;
22-
bridge.webView = webView;
2321
bridge.delegate = delegate;
2422
return bridge;
2523
}
@@ -34,9 +32,18 @@ - (void)callHandler:(NSString*)handlerName parameter:(id)parameter
3432
{
3533
if (parameter != nil)
3634
{
37-
NSBundle *mainB = [NSBundle bundleForClass:[parameter class]];
35+
#pragma clang diagnostic push
36+
#pragma clang diagnostic ignored "-Wunused-variable"
3837
// 参数类型不能为自定义类型
38+
NSBundle *mainB = [NSBundle bundleForClass:[parameter class]];
3939
NSAssert(mainB != [NSBundle mainBundle], @"请使用合法的参数类型");
40+
#pragma clang diagnostic pop
41+
return;
42+
}
43+
else
44+
{
45+
// 执行 JS
46+
[self.context evaluateScript:[NSString stringWithFormat:@"%@()",handlerName]];
4047
return;
4148
}
4249

@@ -98,12 +105,13 @@ - (void)webViewDidStartLoad:(UIWebView *)webView
98105

99106
- (void)webViewDidFinishLoad:(UIWebView *)webView
100107
{
108+
// 获取 JSContext并赋值
101109
_context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
102110

103-
__weak typeof(self) weakself = self;
104-
// 添加所有注册的handel
111+
// 添加所有注册的handler
105112
if (self.messageHandlers.count != 0)
106113
{
114+
__weak typeof(self) weakself = self;
107115
[self.messageHandlers enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
108116
weakself.context[key] = obj;
109117
}];

0 commit comments

Comments
 (0)