Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
AndrewBindon's avatar
AndrewBindon
Explorer
2 years ago

launch the default browser with a URL from a C# app running on PCVR

Can anyone tell me how I can launch the default browser app on the VR headset with a URL from a C# app running on PCVR? ... Or is this not a possible thing to do in PCVR. Maybe I could ask users to install a browser App into their PCVR setup if they don't have one.

In particular I am targeting Oculcus Quest 2 and 3, if that makes any difference. I guess I need something equivalent to Unity's Application.OpenURL() , except I am not implementing a Unity App. Also it is not a Standalone Quest App.

Some people here discussing the issue: https://communityforums.atmeta.com/t5/Quest-Development/Open-URL-in-Oculus-Browser-from-Unity-application/td-p/754582 although I think they are building standalone Oculus Apps, whereas I am building a PCVR app. Also they are using Unity, and I am not using Unity.

1 Reply

  • Anyverse's avatar
    Anyverse
    Heroic Explorer

    Are you using .NET MAUI to create an Android app? Try this...

    using System;
    using Microsoft.Maui.Essentials;
    
    namespace YourNamespace
    {
        public class BrowserOpener
        {
            public async void OpenUrl(string url)
            {
                try
                {
                    // Launches the URL in the system's default browser
                    await Browser.OpenAsync(url, BrowserLaunchMode.SystemPreferred);
                }
                catch (Exception ex)
                {
                    // Handle or log the exception
                    Console.WriteLine($"An error occurred: {ex.Message}");
                }
            }
        }
    }

    If you're using using Unity to target and Android or the Meta Quest version, try this...

    using System.Diagnostics;
    using UnityEngine;
    
    public class OpenURLInQuest : MonoBehaviour
    {
        public void OpenURL(string url)
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                try
                {
                    using (AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"))
                    using (AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent"))
                    {
                        using (AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"))
                        {
                            intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_VIEW"));
                            intentObject.Call<AndroidJavaObject>("setData", uriClass.CallStatic<AndroidJavaObject>("parse", url));
    
                            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                            using (AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                            {
                                currentActivity.Call("startActivity", intentObject);
                            }
                        }
                    }
                }
                catch (System.Exception e)
                {
                    Debug.LogError("Failed to open URL: " + e.Message);
                }
            }
            else
            {
                Debug.LogWarning("URL opening only supported on Android devices.");
            }
        }
    }

    If none of the above work try launching it directly in this manner...

    using System.Diagnostics;
    
    public class OpenUrlExample
    {
        public void OpenWebBrowser(string url)
        {
            Process.Start(url);
        }
    }

    The implementation of WebView for MAUI won't work on the Meta Quest because of differences in their version of Android.

    You can upvote this feature request on GitHub if this is something you're also looking for...

    https://github.com/dotnet/maui/issues/21242