0

I have a webview app done in C# for Android using Xamarin for visual studio. This is supposed to post a message to whatsapp when users click on a button but it shows an error net::ERR_UNKNOWN_URL_SCHEME, please help. the url in question starts with whatsapp://

I have checked this and this one but none of them fits in my code

Here is my full code. am very new to C# for android

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using Android.OS;

namespace IwI
{
    [Activity(Label = "IwI", MainLauncher = true, Theme = "@android:style/Theme.NoTitleBar")]
    public class MainActivity : Activity
    {
        WebView web_view;

        protected override void OnCreate(Bundle bundle)
        {
            RequestWindowFeature(WindowFeatures.NoTitle);
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            web_view = FindViewById<WebView>(Resource.Id.webview);
            web_view.Settings.JavaScriptEnabled = true;
            web_view.SetWebViewClient(new IwIClient());
            web_view.LoadUrl("https://jacidd.com/iwi");

        }
        public class IwIClient : WebViewClient
        {
            public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
            {
                view.LoadUrl(request.Url.ToString());
                return false;
            }
        }
        public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
        {
            if (keyCode == Keycode.Back && web_view.CanGoBack())
            {
                web_view.GoBack();
                return true;
            }
            return base.OnKeyDown(keyCode, e);
        }
    }
}
  • When you get the error, what is the full URI that it's saying is causing a problem, are you definitely escaping characters properly? – JoeTomks May 20 '18 at 10:34
  • full url is https://api.whatsapp.com/send?phone=2547981004616&text= – The Ambidextrous May 21 '18 at 07:46
  • 1
    nobody ever got an answer for this, seems xamarine/C# for android got no community support for beginners, should consider alternatives.. – The Ambidextrous May 25 '18 at 12:26
  • It goes without saying you shouldn't expect swift responses all the time, it depends entirely on your question, and it's tags as to whose going to see it. Occasionally questions sit unanswered for awhile until someone turns up that has the requisite experience. As per you're reply you need to ensure that the URI is fully qualified with https:// at the start, so it should be: https://api.whatsapp.com/send?phone=2547981004616&text= – JoeTomks May 25 '18 at 14:01
  • it starts with https:// what happens that once someone clicks, it changes and starts with whatsapp:// i dont know why – The Ambidextrous May 26 '18 at 10:49

1 Answers1

0

Based off of your replies to my comments, you could try changing your ShouldOverrideUrlLoading method to the one below:

public bool ShouldOverrideUrlLoading(WebView view, string url) 
{
    if (url != null && url.startsWith("whatsapp://")) 
    {
        view.Context.StartActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        return true;
    } 
    else 
    {
        return false;
    }
}

It's also worth taking a look at this answer on a different question, it also might help you solve the problem.

JoeTomks
  • 3,243
  • 1
  • 18
  • 42
  • Shows erros Severity Code Description Project File Line Suppression State Error CS1061 'Intent' does not contain a definition for 'ACTION_VIEW' and no extension method 'ACTION_VIEW' accepting a first argument of type 'Intent' could be found (are you missing a using directive or an assembly reference?) IwI C:\Users\IT\source\repos\IwI\IwI\MainActivity.cs 36 Active – The Ambidextrous May 28 '18 at 15:12
  • and: Severity Code Description Project File Line Suppression State Error CS0117 'Uri' does not contain a definition for 'parse' IwI C:\Users\IT\source\repos\IwI\IwI\MainActivity.cs 36 Active – The Ambidextrous May 28 '18 at 15:13
  • Apologies that was a poorly converted snippet I think the xamarin format is a little different I'll see if I can't find the proper enum names and adjust it when I am next near something other than my mobile phone. – JoeTomks May 28 '18 at 19:50