From 35e75c8cdf036c68680464c8f916bd855ce86c3e Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Wed, 23 Nov 2016 04:41:22 -0800 Subject: [PATCH] Android: Fix WebView crash for links of unknown schemes Summary: When tapping on a link in a WebView with an unknown scheme, the app would crash. For example, if you have the link "something://example/" but your device doesn't have anything to handle the "something" scheme, the app would crash when the user clicks on the link. This change handles the exception to prevent the app from crashing. Instead, the click is a no-op and the WebView doesn't navigate anywhere. **Test plan (required)** Verified the app no longer crashes when clicking on unknown schemes in a test app. Also, my team uses this change in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/10903 Differential Revision: D4226371 Pulled By: mkonicek fbshipit-source-id: a6d3957806c6063e74fe055b0979cb9d1ce40e51 --- .../react/views/webview/ReactWebViewManager.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java index 1a80722fe..650701851 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java @@ -16,6 +16,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Picture; @@ -136,9 +137,13 @@ public class ReactWebViewManager extends SimpleViewManager { url.startsWith("file://")) { return false; } else { - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - view.getContext().startActivity(intent); + try { + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + view.getContext().startActivity(intent); + } catch (ActivityNotFoundException e) { + FLog.w(ReactConstants.TAG, "activity not found to handle uri scheme for: " + url, e); + } return true; } }