Essentials: Browser.CloseAsync() needed for iOS

Created on 12 Apr 2019  路  10Comments  路  Source: xamarin/Essentials

Please can we have Browser functionality extended to Close it problematically? This API functionality exists with iOS and Android.

i.e for iOS extending the implementation for SFSafariViewController like:
sfViewController.DismissViewControllerAsync()

This functionality is required when implementing Single Sign On Solutions. On iOS when you authenticate through a SFSafariViewConroller instance - you use a redirect URL which is registered as a custom URL scheme. When receiving the custom URL, the SFSafariViewController needs to be closed programatically.

This is less of an issue on Android as the CustomTab browser closes automatically when an app is opened with a custom url scheme.

Thanks

feature-request needs-specifications

Most helpful comment

I'm opening a Brower in Xamarin Forms, which redirects me back to the app after completing a form via a custom uri scheme. But the browser tab is still active in the background. (Android and iOS)

Is there any way to close that browser tab?

All 10 comments

@Lydecker - do you know if there is a workaround for this? I have this same issue when trying to perform oAuth in Xamarin Forms app.

@hiraldesai

Hi - sorry for the delayed reply - we ended up having to break away from Essentials due to this limitation, and ended up implementing our own Forms Browser code which implemented sfViewController.DismissViewControllerAsync() for iOS

Thank you @Lydecker - I will check to see if the related enhancement is going to be available any time soon. If not, we will also go down the same path.

FYI and for folks who might stumble upon this thread - Apple rejected our app from publishing because we were using Launcher Essentials API to do OAuth (which launches native browser externally in the app) and it does not conform to their design standard.

I would not use the Launcher API for doing oAuth, look into: https://github.com/Redth/Xamarin.AspNetCore.Authentication

I would just go down the path that @Lydecker until we decide on an API

Thanks @jamesmontemagno - I think this approach makes more sense and gives us more control over the user flow.

Thanks for the suggestion @jamesmontemagno - unfortunately the problem with most pre-built libraries that implement OAUTH - is that they fail to support Proof Key for Code Exchange (i.e support for code_verifiers and code_challenge in the OAUTH process). This is must now for mobile Oauth https://tools.ietf.org/html/rfc7636

Hence the reason why we need to use the Launcher API - which works fine, it's just missing the implementation of sfViewController.DismissViewControllerAsync() !

I'm opening a Brower in Xamarin Forms, which redirects me back to the app after completing a form via a custom uri scheme. But the browser tab is still active in the background. (Android and iOS)

Is there any way to close that browser tab?

In Xamarin Forms, I solved it for iOS by first creating a custom service inside the iOS project:

[assembly: Xamarin.Forms.Dependency(typeof(Xamarin.iOS.Services.PlatformBrowserService))]
namespace Xamarin.iOS.Services
{
    public class PlatformBrowserService : IPlatformBrowserService
    {
        public Task CloseBrowser()
        {
            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController;
            return vc.DismissViewControllerAsync(false);
        }
    }
}

Then, inside Xamarin forms, you can get that instance by using the resolver:

            if (DeviceInfo.Platform == DevicePlatform.iOS)
            {
                var service = DependencyService.Resolve<IPlatformBrowserService>();
                await service.CloseBrowser();
            }

May not be the best but worked for me.

So when I made this issue log initially a year ago, it was for the purpose of implementing Single Sign On (SSO) Logins.

We now have the WebAuthenticator API in the latest version of Essentials which is amazing!

HOWEVER there is still no support for Logging out of a SSO session

Based on #1223 @jamesmontemagno I believe is saying we should still use Browser.Open() to send the logout endpoint to Safari/Custom tabs. Whilst this is great, it still means we need a way to close the Sarafi window on iOS, so even through a Browse.Close() method is no longer required for Logging In, it is still required for Logging Out!

Have I missed something here? Are people expecting their users to manually close a safari window after logging out currently??!

Thanks

Closing this as we have added the WebAuthenticator API. Also, the discussion on logging out is here: #1223

Was this page helpful?
0 / 5 - 0 ratings