google / cordova-plugin-browsertab

A Cordova plugin that provides an interface to in-app browser tabs that exist on some mobile platforms, such as SFSafariViewController on iOS and CustomTabs on Android.
Apache License 2.0
117 stars 147 forks source link

Close method not working and showing ambiguous console warning #18

Open csmadhav opened 7 years ago

csmadhav commented 7 years ago

I am trying to implement OAuth with BrowserTab, I have successfully opened a BrowserTab with .openUrl() and also implemented deeplinks and through that its successfully opens my app and my app continues to work, I am able to pass on the control to my app in Android , But in iOS deeplinks works fine but my app continues in background only BrowserTab remains on foreground and i am trying to close the BrowserTab through close() method it doesn't work and says:

screen shot 2017-06-20 at 11 39 15 am

I am using chrome inspector

here is my code snippet:

public selectTracker(id) {
  id = parseInt(id)
  let url;
  switch(id) {
         case 1 : url = 'http://oauth1.com/';
           break;
         case 2 : url = 'http://oauth2.com/';
           break;
    }

  this.browserTab.openUrl(url);

  (window as any).browserTab = this.browserTab;

  setTimeout(()=>{ // <--- I tried this as well 
      this.browserTab.close();
  },1000);
}

Seems like there is problem with the BrowserTab API since console warning doesn't make sense, I tried removing and installing the plugin again doesn't work.

Also, how should I go about closing the BrowserTab if this isn't the correct way.

murraybauer commented 7 years ago

I am having the same iOS issue (Ionic 3, using https://github.com/EddyVerbruggen/Custom-URL-scheme). The browser remains open in the foreground, but my javascript:alert('URL') dialog from the app which is running in the background actually pops over the browser, but once accepted, the browser still remains. I replaced the JS alert dialog with console.log, but no change. When running from xCode the console output shows no errors.

Android works fine. Originally it would open the browser, close the browser, but would not re open the app (I had to relaunch the app manually which then had the URL available. FYI to solve this I added the following to my config.xml file base upon https://github.com/EddyVerbruggen/Custom-URL-scheme/issues/192: <preference name="AndroidLaunchMode" value="singleInstance" />

@madhavchaturvedi12 Any luck with getting iOS working?

csmadhav commented 7 years ago

Nope. I am using ionic 2, I am trying calling window.close() on the page I want to close BrowserTab, but the problem is, In modern browsers 'scripts may close only the windows that were opened by it', Also on iOS i am facing another issue with BrowserTab that when I open BrowserTab with this.browsertab.openUrl() method nothing happens until I suspend the app then open again, BrowserTab opens automatically after the app resumes. I do not have any other option except BrowserTab, I can't use InAppBrowser (https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/) for OAuth.

murraybauer commented 7 years ago

My use case is for OAuth as well, which as you say InAppBrowser is not suited for (security issues and usability as no shared session state with system browser).

On auth success, my backend sends a HTTP redirect to the custom URL scheme, but the BrowserTab does not close automatically/redirect the user back to the app even though the app receives the call back.

I have also tried the following:

  1. window.close() from within the html page in the tab/popup
  2. browertab.close() from the initiating Ionic app (https://github.com/google/cordova-plugin-browsertab/blob/master/demo/www/js/index.js#L45)
  3. A manual hyperlink to the customURL on the popup HTML page for the user to click. The link does not work (does not even call back to the app in the background). Although the link is correct as it launches the app if I type it manually into a full safari browser.

I'm not experiencing your 2nd issue where the BrowserTab does not launch consistently. Everything works fine for me, expect the the user needs to tap 'Done' to close the BrowserTab after the OAuth flow to return to the app. I using "@ionic-native/browser-tab": "^3.12.1", ionic 3 and testing on iOS 10.2

heberallred commented 5 years ago

We had the same issue with OAuth with BrowserTab until we did a custom URL scheme which for some reason closes the tab when called.

So instead of: window.close(); try myapp://close Note: replace "myapp" with the custom URL scheme of your own app. You set this up using a package such as "cordova-plugin-customurlscheme".

We've had instances where we have to call this twice to get it to close for some reason. The /close part of the custom URL has no known significance, but the "myapp://" part of the URL somehow triggers your app and closes the BrowserTab.

csmadhav commented 5 years ago

WOW! great work around @heberallred

heberallred commented 5 years ago

@ltlbsteed on our team actually found this solution in an article somewhere, and implemented it into our code. I figured I would post here for the benefit of anyone who is trying to solve this issue because it can use up a lot of time to solve. If the main thread of the program didn't freeze while the BrowserTab is open, then it would be a non-issue because the main thread could close it, but for now this is the only work-around we've found.