Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change the code so that you can open sms links #1086

Open
nicknaz opened this issue Aug 9, 2024 · 1 comment
Open

How to change the code so that you can open sms links #1086

nicknaz opened this issue Aug 9, 2024 · 1 comment

Comments

@nicknaz
Copy link

nicknaz commented Aug 9, 2024

Good afternoon! I need to use this webview to make sms links open in the message application. I was given a sample code on kotlin. Please tell me how I can change this webview so that everything works for me.

webView.webViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(
        view: WebView?,
        request: WebResourceRequest?,
    ): Boolean {
        if (request?.url == null || request.url.toString().contains("http://") || request.url.toString().contains("https://"))
            return false
        return try {
            var uri = request.url.toString()
            if (request.url.toString().contains("sms"))
                uri = request.url.toString().replaceFirst("(?<=sms:)/{2}".toRegex(), "")
            Intent(Intent.ACTION_VIEW, Uri.parse(uri)).also { startActivity(it) }
            true
        } catch (e: Exception) {
            true
        }
    }
}

or maybe there is an opportunity to do this in the next commit?

@KojiNakamaru
Copy link
Member

The current code already performs the same thing:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// PackageManager pm = a.getPackageManager();
// List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);
// if (apps.size() > 0) {
// view.getContext().startActivity(intent);
// }
try {
view.getContext().startActivity(intent);
} catch (ActivityNotFoundException ex) {
}

though the following part is not included:

                uri = request.url.toString().replaceFirst("(?<=sms:)/{2}".toRegex(), "")

This url correction seems to convert sms://0123456789 to sms:0123456789. The former is wrong and should not be used, but if you still need to correct it, you can utilize the ld: callback to fix such wrong links, for example:

        webViewObject.Init(
            ld: (msg) =>
            {
                webViewObject.EvaluateJS(@"
webViewObject.EvaluateJS(
Array.from(document.getElementsByTagName('a')).forEach(
    function(e) {
        e.href = e.href.replace(/(?<=sms:)\/{2}/, '');
    });
");
            }
        );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants