首页 > 解决方案 > Xamarin iOS 设备 OpenUri 不打开 Whatsapp uri 方案

问题描述

我在 Iphone 中运行我的 Xamarin Cross 平台并且 Device.OpenUri 不工作,这个函数在 Android 平台上按预期工作,但在 iOS 中,当我点击 whatsapp URI 时,它只会重新加载页面而不打开 Whatsapp应用程序

我尝试在 info.plist 中的 LSApplicationQueriesSchemes 中添加“whatsapp”引用,但未成功

            Hipnosoftpage.IsVisible = false;
            ErroRede = true;

            DisplayAlert("Sem Conexão", "Verifique sua conexão com a internet.", "Tentar Novamente").ContinueWith(t =>
            {
                Hipnosoftpage.Reload();

            }, TaskScheduler.FromCurrentSynchronizationContext());

        }
        var url = e.Url;
        if (url.StartsWith("whatsapp://", StringComparison.InvariantCultureIgnoreCase))
        {
            try
            {
                Device.OpenUri(new Uri(url));
                Hipnosoftpage.GoBack();
            }
            // Can not catch Android exception type in NetStd/PCL library, so hack it...
            catch (Exception ex) when (ex.Message.StartsWith("No Activity found to handle Intent", StringComparison.InvariantCulture))
            {
                // WhatsApp not installed : Android.Content.ActivityNotFoundException: No Activity found to handle Intent
                Console.WriteLine(ex);
            }
        }



    }

我希望 whatsapp 打开,但 Device.OpenUri 没有正确打开,并且在调试控制台中没有给我任何错误。

标签: c#xamarin.forms

解决方案


为了打开whatsapp,您只需要以下内容:

public static void OpenWhatsapp(string phoneNumber, string message = null)
        {
            try
            {
                var uriString = "whatsapp://send?phone=" + phoneNumber;

                if (!string.IsNullOrWhiteSpace(message))
                    uriString += "&text=" + message;

                Device.OpenUri(new Uri(uriString));

            }

检查您的 Uri 格式是否正确。


推荐阅读