Issue
I am using Google forum in website to fill detail, using that website in Webview of android app. When i try to open the Google forum short link in Webview is showing error : net::ERR_UNKNOWN_URL_SCHEME, error code : -10.
And in webview Screen it show this line :
intent://<a href="https://docs.google.com/forms/d/e/##########/viewform%3Fusp%3Dsend_form;end">forms.gle/************#Intent;package=com.google.android.gms;action=com.google.firebase.dynamiclinks.VIEW_DYNAMIC_LINK;scheme=https;S.browser_fallback_url=https://docs.google.com/forms/d/e/########/viewform%3Fusp%3Dsend_form;end</a>; could not be loaded because:<br><br>net::ERR_UNKNOWN_URL_SCHEME
Full Url is working fine, Url in browser is working fine.
Language Used : Kotlin, Android device: Samsung A 10, Android version: 9 (pie)
Code used:
webview.loadUrl("https://forms.gle/#########")
and used these setting:
webview?.getSettings()?.javaScriptCanOpenWindowsAutomatically = true
webview?.getSettings()?.setAppCacheEnabled(true)
webview?.getSettings()?.setAppCachePath(this.cacheDir.path)
webview?.getSettings()?.cacheMode = WebSettings.LOAD_DEFAULT```
Solution
To fix these type of Issues , we have to ovveride shouldOverrideUrlLoading method of the WebView Client that we set to Webview. Sometimes the overriding urls can start with intent, so they have to be handled in other way. Attached the sample Code below.
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startsWith("http:") || url.startsWith("https:")) {
return false;
} else {
if (url.startsWith("intent://")) {
try {
Context context = webView.getContext();
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
if (intent != null) {
PackageManager packageManager = context.getPackageManager();
ResolveInfo info = packageManager.resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY);
// This IF statement can be omitted if you are not strict about
// opening the Google form url in WebView & can be opened in an
// External Browser
if ((intent != null) && ((intent.getScheme().equals("https"))
|| (intent.getScheme().equals("http")))) {
String fallbackUrl = intent.getStringExtra(
"browser_fallback_url");
webView.loadUrl(fallbackUrl);
return true;
}
if (info != null) {
context.startActivity(intent);
} else {
// Call external broswer
String fallbackUrl = intent.getStringExtra(
"browser_fallback_url");
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(fallbackUrl));
context.startActivity(browserIntent);
}
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
webView.getContext().startActivity(intent);
return true;
}
}
}
});
Answered By - Sasank Sunkavalli
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.