Ian W
Ian W

Reputation: 411

Close browser after calling http trigger in .NET MAUI app

Is it possible to close a browser after I have opened it in a .NET MAUI Visual Studio app?

My use case is when I press a button in the app (very simple app) I call a URL, this URL is a trigger URL for an Alexa device. This part works fine, but the browser persists on the phone and I would really like to close the tab I have just opened (or even the browser)

My code called on the click event

 private async void CallUrlTrigger_NFC_TAG1_off()
 {
     try
     {
         Uri uri = newUri("https://www.virtualsmarthome.xyz/url_routine_trigger/activate.php?trigger=5a67c570-d535-4fa4-88cc-18817d064ce2&token=75ae5653-81b1-45f9-a6f9-0523cdb0c716&response=html");
         BrowserLaunchOptions options = new BrowserLaunchOptions()
         {
             LaunchMode = BrowserLaunchMode.SystemPreferred,
             TitleMode = BrowserTitleMode.Show,
             PreferredToolbarColor = Colors.Violet,
             PreferredControlColor = Colors.SandyBrown
         };

         await Browser.Default.OpenAsync(uri, options);

     //block of code to close the tab just opened or the browser

     }
     catch (Exception ex)
     {
         // An unexpected error occurred. No browser may be installed on the device.
         await DisplayAlert("Alert", "Unable to trigger the lights" + ex.Message, "OK");
     }
 }

Upvotes: 0

Views: 742

Answers (2)

hankor
hankor

Reputation: 400

BackButtonPressed solution did not work for me on iOS. What did work was:

WindowStateManager.Default.GetCurrentUIViewController().DismissViewController(true, () => {})

Upvotes: 1

Jerry Berry
Jerry Berry

Reputation: 26

You can navigate back to close the browser. For example:

Shell.Current.SendBackButtonPressed();

I've only tested it on android but it should work on all platforms.

Upvotes: 1

Related Questions