Skip to content

Commit

Permalink
Merge pull request #75 from gree/feature/error_callback_for_load_failure
Browse files Browse the repository at this point in the history
CallOnError callback (currently for page load failures)
  • Loading branch information
KojiNakamaru committed May 24, 2016
2 parents 770f34b + 3262706 commit ab3c5a1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 21 deletions.
Binary file modified dist/unity-webview.unitypackage
Binary file not shown.
Binary file modified dist/unity-webview.zip
Binary file not shown.
15 changes: 10 additions & 5 deletions plugins/Android/src/net/gree/unitywebview/CWebViewPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ public CWebViewPluginInterface(CWebViewPlugin plugin, String gameObject) {
}

@JavascriptInterface
public void call(String message) {
final String msg = message;
public void call(final String method, final String message) {
final Activity a = UnityPlayer.currentActivity;
a.runOnUiThread(new Runnable() {public void run() {
if (mPlugin.IsInitialized()) {
UnityPlayer.UnitySendMessage(mGameObject, "CallFromJS", msg);
UnityPlayer.UnitySendMessage(mGameObject, method, message);
}
}});
}
Expand All @@ -81,7 +80,7 @@ public void Init(final String gameObject, final boolean transparent) {
if (mWebView != null) {
return;
}
WebView webView = new WebView(a);
final WebView webView = new WebView(a);
webView.setVisibility(View.GONE);
webView.setFocusable(true);
webView.setFocusableInTouchMode(true);
Expand All @@ -96,6 +95,12 @@ public void Init(final String gameObject, final boolean transparent) {

mWebViewPlugin = new CWebViewPluginInterface(self, gameObject);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("about:blank");
mWebViewPlugin.call("CallOnError", errorCode + "\t" + description + "\t" + failingUrl);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://") || url.startsWith("https://")
Expand All @@ -104,7 +109,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
} else if (url.startsWith("unity:")) {
String message = url.substring(6);
mWebViewPlugin.call(message);
mWebViewPlugin.call("CallFromJS", message);
return true;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Expand Down
9 changes: 7 additions & 2 deletions plugins/Mac/Sources/WebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ - (id)initWithGameObject:(const char *)gameObject_ transparent:(BOOL)transparent
[webView setDrawsBackground:NO];
}
[webView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
[webView setFrameLoadDelegate:(id)self];
[webView setPolicyDelegate:(id)self];
gameObject = [[NSString stringWithUTF8String:gameObject_] retain];
if (ua_ != NULL && strcmp(ua_, "") != 0) {
Expand Down Expand Up @@ -143,12 +144,16 @@ - (void)dealloc
[super dealloc];
}

- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
{
UnitySendMessage([gameObject UTF8String], "CallOnError", [[error description] UTF8String]);
}

- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
{
NSString *url = [[request URL] absoluteString];
if ([url hasPrefix:@"unity:"]) {
UnitySendMessage([gameObject UTF8String],
"CallFromJS", [[url substringFromIndex:6] UTF8String]);
UnitySendMessage([gameObject UTF8String], "CallFromJS", [[url substringFromIndex:6] UTF8String]);
[listener ignore];
} else {
[listener use];
Expand Down
21 changes: 15 additions & 6 deletions plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static void Dispatch(string name, string method, string message)

public class WebViewObject : MonoBehaviour
{
Callback callback;
Callback onJS;
Callback onError;
bool visibility;
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
IntPtr webView;
Expand Down Expand Up @@ -137,12 +138,13 @@ private static extern void _CWebViewPlugin_SetFrame(
#endif

#if UNITY_EDITOR || UNITY_STANDALONE_OSX
public void Init(Callback cb = null, bool transparent = false, string ua = @"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53")
public void Init(Callback cb = null, bool transparent = false, string ua = @"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53", Callback err = null)
#else
public void Init(Callback cb = null, bool transparent = false)
public void Init(Callback cb = null, bool transparent = false, Callback err = null)
#endif
{
callback = cb;
onJS = cb;
onError = err;
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
{
var path = Regex.Replace(_CWebViewPlugin_GetAppPath() + "Contents/Info.plist", "^file://", "");
Expand Down Expand Up @@ -296,13 +298,20 @@ public void EvaluateJS(string js)
#endif
}

public void CallOnError(string message)
{
if (onError != null) {
onError(message);
}
}

public void CallFromJS(string message)
{
if (callback != null) {
if (onJS != null) {
#if !UNITY_ANDROID
message = WWW.UnEscapeURL(message);
#endif
callback(message);
onJS(message);
}
}

Expand Down
5 changes: 5 additions & 0 deletions plugins/iOS/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ - (void)dealloc
gameObjectName = nil;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UnitySendMessage([gameObjectName UTF8String], "CallOnError", [[error description] UTF8String]);
}

- (BOOL)webView:(UIWebView *)uiWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (webView == nil)
Expand Down
22 changes: 14 additions & 8 deletions sample/Assets/Scripts/SampleWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
public class SampleWebView : MonoBehaviour
{
public string Url;
public string SameDomainUrl;
public GUIText status;
WebViewObject webViewObject;

Expand All @@ -34,13 +33,20 @@ IEnumerator Start()
void Start()
#endif
{
webViewObject =
(new GameObject("WebViewObject")).AddComponent<WebViewObject>();
webViewObject.Init((msg)=>{
Debug.Log(string.Format("CallFromJS[{0}]", msg));
status.text = msg;
status.GetComponent<Animation>().Play();
});
webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
webViewObject.Init(
cb: (msg) =>
{
Debug.Log(string.Format("CallFromJS[{0}]", msg));
status.text = msg;
status.GetComponent<Animation>().Play();
},
err: (msg) =>
{
Debug.Log(string.Format("CallOnError[{0}]", msg));
status.text = msg;
status.GetComponent<Animation>().Play();
});

webViewObject.SetMargins(5, 5, 5, Screen.height / 4);
webViewObject.SetVisibility(true);
Expand Down

0 comments on commit ab3c5a1

Please sign in to comment.