swan-io / react-native-browser

An easy-to-use in-app browser module for React Native, powered by Chrome Custom Tabs / SFSafariViewController.
MIT License
135 stars 11 forks source link

Add onRedirect callback #2

Closed c100k closed 8 months ago

c100k commented 8 months ago

Why it is needed?

From the docs :

Once the redirect URL is visited (a GET hits your server), handle the result and perform a server redirect to com.company.myapp://close?success=true to close the browser (and pass any data back to your app using query params ✨).

This works well when we manage the server behind the webpage being opened, but not when it's a third party website.

For example, in an "OAuth dance", the third party will redirect to the redirect URL we defined when declaring the application on their side. Some of them do not allow to put a deeplink there, forcing an HTTP URL. Which makes it impossible to have the onClose callback called with the deeplink. I can link my mobile app present in the stores to illustrate if need be.

Possible implementation

One of the solutions I imagined is to have an onRedirect callback, having the same signature as onClose. This way, on the mobile app side, we could listen to the URL changes and mimick the onClose callback when needed.

Code sample

import { openBrowser } from "@swan-io/react-native-browser";

openBrowser("https://swan.io", {
  dismissButtonStyle: "close", // "cancel" | "close" | "done" (default to "close")
  barTintColor: "#FFF", // in-app browser UI background color
  controlTintColor: "#000", // in-app browser buttons color
  onOpen: () => {
    // fired on browser opened
    // useful to switch the StatusBar color, for example
  },
  onClose: (url) => {
    // fired on browser closed
    // url will be defined if the browser has been closed via deeplink
  },
  onRedirect: (url) => {
    // fired on each page redirect within the browser
    // url corresponds to the new URL of the browser (i.e. window.location.href)
  },
}).catch((error) => {
  console.error(error);
});
zoontek commented 8 months ago

@c100k You can't do that without a dedicated deeplink (at least in in-app browsers)

c100k commented 8 months ago

Ah ! Indeed : https://stackoverflow.com/questions/42192701/detect-url-change-in-sfsafariviewcontroller

Closing then. Thanks for your quick reply.