Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cordova InAppBrowser does not work with _system param

I'm trying to open a payment page with cordova InAppBrowser and i want to open that page in system browser on mobile devices. I'm also try the _blank param but _blank just open that page in the same window to app. And i also want to Send Post Request over Cordova InAppBrowser. this is my code:

    var redirect = 'https://SomeRef';

    var pageContent = '<form id="FormID" action="https://SomeOtherRefs" method="post">' +
      '<input type="hidden" name="RedirectURL" value="' + redirect + '">' +
      '<input type="hidden" name="Token" value="' + dataVar + '">' +
      '</form> <script type="text/javascript">document.getElementById("FormID").submit();</script>';
    var pageContentUrl = 'data:text/html;base64,' + btoa(pageContent);

    var browserRef = cordova.InAppBrowser.open(
      pageContentUrl,
      "_system",
      "hidden=no,location=no,clearsessioncache=yes,clearcache=yes"
    );

There is no action from this with _system param, and _blank just open the page in the same window to app. What should i do for open the payment page in system browser of device?

like image 598
omid nematollahi Avatar asked Dec 15 '18 07:12

omid nematollahi


People also ask

How do I use InAppBrowser in Cordova?

function openBrowser() { var url = 'https://cordova.apache.org'; var target = '_blank'; var options = "location = yes" var ref = cordova. InAppBrowser. open(url, target, options); ref. addEventListener('loadstart', loadstartCallback); ref.

How do I hide my URL in InAppBrowser?

you can hide the toolbar using the 'toolbar' option. you just need to set location to no and it'll hide the URL bar.

What is InAppBrowser in Android?

The InAppBrowser is a web browser view that displays when calling [window. open](window. open. html)() , or when opening a link formed as <a target="_blank"> .


2 Answers

Finally I have found the solution in this branch of original InAppBrowser repository.

Anyone who has the same problem take a look at openExternal function of this branch. It allows data be opened like an external link.

public String openExternal(String url) {
    try {
        // Omitting the MIME type for file: URLs causes "No Activity found to handle Intent".
        // Adding the MIME type to http: URLs causes them to not be handled by the downloader.
        Uri uri = Uri.parse(url);
        String scheme = uri.getScheme();

        Intent intent = "data".equals(scheme)
                ? Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER)
                : new Intent(Intent.ACTION_VIEW);

        if ("file".equals(scheme)) {
            intent.setDataAndType(uri, webView.getResourceApi().getMimeType(uri));
        } else {
            intent.setData(uri);
        }

        intent.putExtra(Browser.EXTRA_APPLICATION_ID, cordova.getActivity().getPackageName());
        this.cordova.getActivity().startActivity(intent);
        return "";
        // not catching FileUriExposedException explicitly because buildtools<24 doesn't know about it
    } catch (java.lang.RuntimeException e) {
        LOG.d(LOG_TAG, "InAppBrowser: Error loading url " + url + ":" + e.toString());
        return e.toString();
    }
}

After using the above function, everything is going right.

like image 107
omid nematollahi Avatar answered Sep 22 '22 14:09

omid nematollahi


I have found that you have two problems (if there are more, explain them, so that I will update the answer):

  1. How to post data to an inappbrowser with _system config.

    • For the first part, you can try this answer. In this solution, you will open up a page containing the data in its html and then call its submit event (using post method on form) to post data to you desired page.
  2. And how to return from opened page, to the opener application.

    • And for the second one try using deeplinks. For example using this plugin. Using deeplinks, your application will receive the broadcasts of URL opening. So that, from the opened page, redirect to a page containing link to x and set x as deeplink to your app page. I think deeplinks does not work on all mobile devices but I think this is the only way by now.
like image 21
ConductedClever Avatar answered Sep 20 '22 14:09

ConductedClever