@@ -16,6 +16,7 @@ @implementation AppDelegate {
16
16
WKWebView *_WKWebView;
17
17
WebViewJavascriptBridge* _bridge;
18
18
WKWebViewJavascriptBridge* _WKBridge;
19
+ NSView * _WKWebViewWrapper;
19
20
}
20
21
21
22
- (void )applicationDidFinishLaunching : (NSNotification *)aNotification
@@ -58,6 +59,13 @@ - (void)_configureWebview {
58
59
[callbackButton setAction: @selector (_callHandler )];
59
60
[_webView addSubview: callbackButton];
60
61
62
+ NSButton *webViewToggleButton = [[NSButton alloc ] initWithFrame: NSMakeRect (235 , 0 , 180 , 40 )];
63
+ [webViewToggleButton setTitle: @" Switch to WKWebView" ];
64
+ [webViewToggleButton setBezelStyle: NSRoundedBezelStyle ];
65
+ [webViewToggleButton setTarget: self ];
66
+ [webViewToggleButton setAction: @selector (_toggleExample )];
67
+ [_webView addSubview: webViewToggleButton];
68
+
61
69
62
70
// Load Page
63
71
NSString * htmlPath = [[NSBundle mainBundle ] pathForResource: @" ExampleApp" ofType: @" html" ];
@@ -67,12 +75,6 @@ - (void)_configureWebview {
67
75
68
76
69
77
- (void )_configureWKWebview {
70
-
71
- // Load Page
72
- NSString * htmlPath = [[NSBundle mainBundle ] pathForResource: @" ExampleApp" ofType: @" html" ];
73
- NSString * html = [NSString stringWithContentsOfFile: htmlPath encoding: NSUTF8StringEncoding error: nil ];
74
- [_WKWebView loadHTMLString: html baseURL: nil ];
75
-
76
78
// Create Bridge
77
79
_WKBridge = [WKWebViewJavascriptBridge bridgeForWebView: _WKWebView handler: ^(id data, WVJBResponseCallback responseCallback) {
78
80
NSLog (@" ObjC received message from JS: %@ " , data);
@@ -104,6 +106,23 @@ - (void)_configureWKWebview {
104
106
[callbackButton setTarget: self ];
105
107
[callbackButton setAction: @selector (_WKCallHandler )];
106
108
[_WKWebView addSubview: callbackButton];
109
+
110
+ NSButton *webViewToggleButton = [[NSButton alloc ] initWithFrame: NSMakeRect (235 , 0 , 180 , 40 )];
111
+ [webViewToggleButton setTitle: @" Switch to WebView" ];
112
+ [webViewToggleButton setBezelStyle: NSRoundedBezelStyle ];
113
+ [webViewToggleButton setTarget: self ];
114
+ [webViewToggleButton setAction: @selector (_toggleExample )];
115
+ [_WKWebView addSubview: webViewToggleButton];
116
+
117
+ // Load Page
118
+ NSString * htmlPath = [[NSBundle mainBundle ] pathForResource: @" ExampleApp" ofType: @" html" ];
119
+ NSString * html = [NSString stringWithContentsOfFile: htmlPath encoding: NSUTF8StringEncoding error: nil ];
120
+ [_WKWebView loadHTMLString: html baseURL: nil ];
121
+ }
122
+
123
+ -(void )_toggleExample {
124
+ _WKWebView.hidden = !_WKWebView.isHidden ;
125
+ _webView.hidden = !_webView.isHidden ;
107
126
}
108
127
109
128
- (void )_sendMessage {
@@ -137,29 +156,14 @@ - (void)_createViews {
137
156
// WebView
138
157
_webView = [[WebView alloc ] initWithFrame: contentView.frame];
139
158
[_webView setAutoresizingMask: (NSViewHeightSizable | NSViewWidthSizable)];
159
+ _webView.hidden = YES ;
140
160
141
161
// WKWebView
142
162
_WKWebView = [[WKWebView alloc ] initWithFrame: contentView.frame];
143
163
[_WKWebView setAutoresizingMask: (NSViewHeightSizable | NSViewWidthSizable)];
144
164
145
- // Tabs
146
- NSTabView *tabView = [[NSTabView alloc ]
147
- initWithFrame: contentView.frame];
148
- [contentView addSubview: tabView];
149
-
150
- NSTabViewItem *tab1 = [[NSTabViewItem alloc ]
151
- initWithIdentifier: @" tab1" ];
152
- [tab1 setLabel: @" WebView" ];
153
- [tabView addTabViewItem: tab1];
154
-
155
- NSTabViewItem *tab2 = [[NSTabViewItem alloc ]
156
- initWithIdentifier: @" tab2" ];
157
- [tab2 setLabel: @" WKWebView" ];
158
- [tabView addTabViewItem: tab2];
159
-
160
- // Initialize each tab
161
- [tab1 setView: _webView];
162
- [tab2 setView: _WKWebView];
165
+ [contentView addSubview: _WKWebView];
166
+ [contentView addSubview: _webView];
163
167
}
164
168
165
169
0 commit comments