nativescript-advanced-webview icon indicating copy to clipboard operation
nativescript-advanced-webview copied to clipboard

Add Ability to Close WebView

Open one-adam-nolan opened this issue 7 years ago • 8 comments

This would be really helpful for OpenId Connect using a Hybrid Authorization Workflow. For my case, I launch to the external browser for login (Identity Server 4) and use the redirect with UrlHandler. If the server redirect is something like adams.app://authorized I want to programmatically be able to close the webview and pop back into the app.

I have a created a branch locally and have implemented the dismiss call for the SafariViewController (iOS) in the Demo app. Unfortunately, its been sometime since I have dealt with native Android; so I am not sure where to start there.

one-adam-nolan avatar Feb 18 '18 19:02 one-adam-nolan

Hello @one-adam-nolan , I need exactly the same thing and I am interested in your implementation of the dismiss call for SafariViewController. Can you please provide a link to your branch? I checked this repository but I couldn't find the implementation. Thanks.

alexmeia avatar May 21 '18 09:05 alexmeia

@alexmeia Sorry about not getting back with you sooner. I need to check my work computer to see if I still have that repo locally. At the time, I hadn't really used git and had some trouble setting up my fork.

one-adam-nolan avatar May 26 '18 14:05 one-adam-nolan

I'm using this plugin to implement an OAuth flow and ran into this issue aswell. After some searching i found the following solution, which seems to work and close the SafariViewController.

This might help to implement the close() feature or for other people having the same issue.

import * as application from 'application';

// Only for IOS
const controller = application.ios.nativeApp.windows[0].rootViewController;
controller.dismissViewControllerAnimatedCompletion(true, () => {
  console.log('Safari dismissed')
});

Aqu1nt avatar Jun 18 '18 11:06 Aqu1nt

thanks @Aqu1nt that works !!

championswimmer avatar Jul 11 '18 15:07 championswimmer

@bradmartin I'll try to implement the same in Android, and if theres a reliable way to do both in Android and iOS, you can maybe add a method in the library itself that does it !

championswimmer avatar Jul 11 '18 15:07 championswimmer

@championswimmer the lib is => here feel free to send a pr 😄

triniwiz avatar Jul 11 '18 16:07 triniwiz

actually if launchMode is singleTop in Android, it automatically closes!

championswimmer avatar Jul 21 '18 20:07 championswimmer

Two months ago I was in a rush to make it close the advanced webview in iOS after login, and I did a little edit to the plugin to return the SFSafariViewController (or the AdvancedWebView in Android) when the method openAdvancedUrl is called. In this way, you can close the WebView calling the native methods (in my case, as @championswimmer said, this was needed only in iOS).

let advancedWebView: any;
advancedWebView = openAdvancedUrl(opts);

export function closeAdvancedWebView(animated: boolean) {
    // No need to manually close advancedWebView in Android.
    if (advancedWebView && platform.isIOS) {
        advancedWebView.dismissViewControllerAnimatedCompletion(animated, null);
    }
}

If this can be useful, you can see iOS the edit here: https://github.com/phoops/nativescript-advanced-webview/blob/master/advanced-webview.ios.ts#L79 Pretty same for Android. I'm using our custom version as dependency in my projects, for now.

alexmeia avatar Jul 21 '18 23:07 alexmeia