question

JonasRembratt-1434 avatar image
0 Votes"
JonasRembratt-1434 asked YonglunLiu-MSFT commented

How to close browser tab with Xamarin.Android?

So, I wrote a Nuget that allows dev in my company to quickly setup authorization using OIDC. Part of that, of course, is handing off the authentication step to a browser so I'm simply invoking Browse.OpenAsync with LaunchMode = BrowserLaunchMode.SystemPreferred. I also set up a HTTP listener that awaits the ahtority's authorization code callback. All works find but the browser window is of course still sitting at the top, hiding the initiating app. I do realise I can use deep linking to simply "call up" the app again but that has two implications I would like to avoid: The need to register schema uri with the mobile OS and, above all, the need to register that (weird) callback URI with my company's API management system (which doesn't like anything but "http" or "https" URL schemes. Those requirements makes it more complicated for our devs to get started and, like I mentioned, it doesn't work very well with our API management system, so I'm looking for a simpler solution.

I have solved this for iOS by simply fetching the key window's view controller and dismissing it, like so:

async Task<Outcome> IPlatformService.TryCloseTopWindowAsync(bool isModalWindow, bool animated)
{
    var window = UIApplication.SharedApplication.KeyWindow;
    var viewController = window?.RootViewController;
    if (viewController is null)
        return Outcome.Fail("Couldn't obtain top window view controller");

    try
    {
        if (isModalWindow)
        {
            viewController.DismissModalViewController(animated);
            return Outcome.Success();
        }
        await viewController.DismissViewControllerAsync(animated);
            return Outcome.Success();
    }
    catch (Exception ex)
    {
        return Outcome.Fail(ex);
    }
}


Unfortunately, I'm not very good with Android's (Java based) APIs so I'm wondering if it's possible to do something similar here? Can I get the top app (activated by my own app) and dismiss it? If so; how?

dotnet-xamarin
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

On Android, as the CustomTab browser closes automatically when an app is opened with a custom URI scheme.

The need to register schema uri with the mobile OS and, above all, the need to register that (weird) callback URI with my company's API management system (which doesn't like anything but "http" or "https" URL schemes.

I noticed this, therefore, if you want to close automatically, you could use WebView to implement the close function.

In WebView, you could use PopAsync() or Finish() to jump to the previous activity.

0 Votes 0 ·

The problem with a WebView is it's not allowed access to device certificates, which is used by our company's SSO mechanism.

0 Votes 0 ·

On Android, there is no such function like DismissViewControllerAsync(). We could not control the life-cycle of other application.

After Android API 21, the method has been onReceivedClientCertRequest added for notifying the host application to handle an SSL client certificate request. Is this what you need?

0 Votes 0 ·
Show more comments

0 Answers