Skip to content

Commit 37d2338

Browse files
committed
introduced experimental support for new windows (target="_black").
1 parent a854c8a commit 37d2338

File tree

2 files changed

+142
-37
lines changed

2 files changed

+142
-37
lines changed

plugins/Android/src/net/gree/unitywebview/CWebViewPlugin.java

Lines changed: 111 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
import android.webkit.CookieSyncManager;
4545
import android.widget.FrameLayout;
4646

47+
import android.app.AlertDialog;
48+
import android.content.DialogInterface;
49+
import android.os.Message;
50+
import android.view.WindowManager;
51+
import android.util.Log;
52+
4753
import java.net.HttpURLConnection;
4854
import java.net.URL;
4955
import java.net.URLEncoder;
@@ -80,6 +86,8 @@ public void call(final String method, final String message) {
8086
public class CWebViewPlugin {
8187
private static FrameLayout layout = null;
8288
private WebView mWebView;
89+
private WebView mPopWebView;
90+
private View mVideoView;
8391
private CWebViewPluginInterface mWebViewPlugin;
8492
private int progress;
8593
private boolean canGoBack;
@@ -94,6 +102,102 @@ public boolean IsInitialized() {
94102
return mWebView != null;
95103
}
96104

105+
class UriChromeClient extends WebChromeClient {
106+
// cf. https://github.com/hwasiti/Android_Popup_Webview_handler_example
107+
@Override
108+
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
109+
if (mPopWebView != null) {
110+
return false;
111+
}
112+
Activity a = UnityPlayer.currentActivity;
113+
mPopWebView = new WebView(a);
114+
// mPopWebView.setVerticalScrollBarEnabled(false);
115+
// mPopWebView.setHorizontalScrollBarEnabled(false);
116+
mPopWebView.setWebViewClient(new WebViewClient());
117+
mPopWebView.setWebChromeClient(new UriChromeClient());
118+
WebSettings webSettings = mPopWebView.getSettings();
119+
webSettings.setSupportZoom(true);
120+
webSettings.setBuiltInZoomControls(true);
121+
webSettings.setDisplayZoomControls(false);
122+
webSettings.setLoadWithOverviewMode(true);
123+
webSettings.setUseWideViewPort(true);
124+
webSettings.setJavaScriptEnabled(true);
125+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
126+
// Log.i("CWebViewPlugin", "Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT);
127+
webSettings.setAllowUniversalAccessFromFileURLs(true);
128+
}
129+
webSettings.setMediaPlaybackRequiresUserGesture(false);
130+
webSettings.setDatabaseEnabled(true);
131+
webSettings.setDomStorageEnabled(true);
132+
String databasePath = mPopWebView.getContext().getDir("databases", Context.MODE_PRIVATE).getPath();
133+
webSettings.setDatabasePath(databasePath);
134+
// webSettings.setAppCacheEnabled(true);
135+
// webSettings.setSavePassword(true);
136+
// webSettings.setSaveFormData(true);
137+
138+
layout.addView(
139+
mPopWebView,
140+
new FrameLayout.LayoutParams(
141+
LayoutParams.MATCH_PARENT,
142+
LayoutParams.MATCH_PARENT,
143+
Gravity.NO_GRAVITY));
144+
mPopWebView.setLayoutParams(mWebView.getLayoutParams());
145+
CookieManager cookieManager = CookieManager.getInstance();
146+
cookieManager.setAcceptCookie(true);
147+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
148+
cookieManager.setAcceptThirdPartyCookies(mPopWebView, true);
149+
}
150+
WebView.WebViewTransport transport = (WebView.WebViewTransport)resultMsg.obj;
151+
transport.setWebView(mPopWebView);
152+
resultMsg.sendToTarget();
153+
return true;
154+
}
155+
156+
@Override
157+
public void onCloseWindow(WebView window) {
158+
try {
159+
layout.removeView(mPopWebView);
160+
} catch (Exception e) {
161+
}
162+
try {
163+
mPopWebView.destroy();
164+
} catch (Exception e) {
165+
}
166+
mPopWebView = null;
167+
}
168+
169+
@Override
170+
public void onProgressChanged(WebView view, int newProgress) {
171+
progress = newProgress;
172+
}
173+
174+
@Override
175+
public void onShowCustomView(View view, CustomViewCallback callback) {
176+
super.onShowCustomView(view, callback);
177+
if (layout != null) {
178+
mVideoView = view;
179+
layout.setBackgroundColor(0xff000000);
180+
layout.addView(mVideoView);
181+
}
182+
}
183+
184+
@Override
185+
public void onHideCustomView() {
186+
super.onHideCustomView();
187+
if (layout != null) {
188+
layout.removeView(mVideoView);
189+
layout.setBackgroundColor(0x00000000);
190+
mVideoView = null;
191+
}
192+
}
193+
194+
// @Override
195+
// public boolean onConsoleMessage(android.webkit.ConsoleMessage cm) {
196+
// Log.d("Webview", cm.message());
197+
// return true;
198+
// }
199+
}
200+
97201
public void Init(final String gameObject, final boolean transparent, final String ua) {
98202
final CWebViewPlugin self = this;
99203
final Activity a = UnityPlayer.currentActivity;
@@ -108,40 +212,7 @@ public void Init(final String gameObject, final boolean transparent, final Strin
108212
webView.setFocusable(true);
109213
webView.setFocusableInTouchMode(true);
110214

111-
// webView.setWebChromeClient(new WebChromeClient() {
112-
// public boolean onConsoleMessage(android.webkit.ConsoleMessage cm) {
113-
// Log.d("Webview", cm.message());
114-
// return true;
115-
// }
116-
// });
117-
webView.setWebChromeClient(new WebChromeClient() {
118-
View videoView;
119-
120-
@Override
121-
public void onProgressChanged(WebView view, int newProgress) {
122-
progress = newProgress;
123-
}
124-
125-
@Override
126-
public void onShowCustomView(View view, CustomViewCallback callback) {
127-
super.onShowCustomView(view, callback);
128-
if (layout != null) {
129-
videoView = view;
130-
layout.setBackgroundColor(0xff000000);
131-
layout.addView(videoView);
132-
}
133-
}
134-
135-
@Override
136-
public void onHideCustomView() {
137-
super.onHideCustomView();
138-
if (layout != null) {
139-
layout.removeView(videoView);
140-
layout.setBackgroundColor(0x00000000);
141-
videoView = null;
142-
}
143-
}
144-
});
215+
webView.setWebChromeClient(new UriChromeClient());
145216

146217
mWebViewPlugin = new CWebViewPluginInterface(self, gameObject);
147218
webView.setWebViewClient(new WebViewClient() {
@@ -155,7 +226,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
155226

156227
@Override
157228
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
158-
canGoBack = webView.canGoBack();
229+
canGoBack = webView.canGoBack();
159230
canGoForward = webView.canGoForward();
160231
mWebViewPlugin.call("CallOnHttpError", Integer.toString(errorResponse.getStatusCode()));
161232
}
@@ -253,6 +324,11 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
253324
webSettings.setDomStorageEnabled(true);
254325
String databasePath = webView.getContext().getDir("databases", Context.MODE_PRIVATE).getPath();
255326
webSettings.setDatabasePath(databasePath);
327+
// webSettings.setAppCacheEnabled(true);
328+
// webSettings.setSavePassword(true);
329+
// webSettings.setSaveFormData(true);
330+
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
331+
webSettings.setSupportMultipleWindows(true);
256332

257333
if (transparent) {
258334
webView.setBackgroundColor(0x00000000);

plugins/iOS/WebView.mm

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void
120120
@interface CWebViewPlugin : NSObject<UIWebViewDelegate, WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>
121121
{
122122
UIView <WebViewProtocol> *webView;
123+
WKWebView *popWebView;
123124
NSString *gameObjectName;
124125
NSMutableDictionary *customRequestHeader;
125126
}
@@ -297,6 +298,33 @@ - (BOOL)webView:(UIWebView *)uiWebView shouldStartLoadWithRequest:(NSURLRequest
297298
}
298299
}
299300

301+
- (WKWebView *)webView:(WKWebView *)wkWebView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
302+
if (popWebView) {
303+
return nil;
304+
}
305+
popWebView = [[WKWebView alloc] initWithFrame:webView.frame configuration:configuration];
306+
popWebView.frame
307+
= CGRectMake(
308+
webView.frame.origin.x,
309+
webView.frame.origin.y,
310+
webView.frame.size.width,
311+
webView.frame.size.height);
312+
popWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
313+
popWebView.UIDelegate = self;
314+
popWebView.navigationDelegate = self;
315+
// popWebView.load(navigationAction.request);
316+
UIView *view = UnityGetGLViewController().view;
317+
[view addSubview:popWebView];
318+
return popWebView;
319+
}
320+
321+
- (void)webViewDidClose:(WKWebView *)webView {
322+
if (webView == popWebView) {
323+
[popWebView removeFromSuperview];
324+
popWebView = nil;
325+
}
326+
}
327+
300328
- (void)webView:(WKWebView *)wkWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
301329
{
302330
if (webView == nil) {
@@ -329,8 +357,9 @@ - (void)webView:(WKWebView *)wkWebView decidePolicyForNavigationAction:(WKNaviga
329357
} else if (navigationAction.navigationType == WKNavigationTypeLinkActivated
330358
&& (!navigationAction.targetFrame || !navigationAction.targetFrame.isMainFrame)) {
331359
// cf. for target="_blank", cf. http://qiita.com/ShingoFukuyama/items/b3a1441025a36ab7659c
332-
[webView load:navigationAction.request];
333-
decisionHandler(WKNavigationActionPolicyCancel);
360+
// [webView load:navigationAction.request];
361+
// decisionHandler(WKNavigationActionPolicyCancel);
362+
decisionHandler(WKNavigationActionPolicyAllow);
334363
return;
335364
} else {
336365
if (navigationAction.targetFrame != nil && navigationAction.targetFrame.isMainFrame) {

0 commit comments

Comments
 (0)