如何从 Cordova 的应用内浏览器获取数据

How to get data from Cordova's in-App Browser

我正在为 Ionic 4 应用程序进行身份验证。我正在使用应用内浏览器插件登录。成功登录后,访问令牌将发送回客户端。我不知道如何从应用程序内浏览器获取令牌到应用程序。

您可以使用事件侦听器,例如:

this.browser.on('loadstart').subscribe(event => { this.checkForToken(event); })

其中 checkForToken 可以查看您的 "event.url" 并解析要存储到 localStorage 的令牌或应用程序内浏览器关闭后用于登录的其他方法。

使用事件来监听负载启动和停止是一个很棒的主意。然而,我面临着另一个问题。 Google 不允许使用应用内浏览器登录,而我确实需要它。在四处询问并查看类似的过去问题后,我遇到了一个名为 BrowserTab. This was awesome as it's an instance/Custom Tab from chrome so it worked fine with google sign in. Another problem came up. You can't inject Javascript into BrowserTab and there was no way to programmatically close it once done. I looked around some more and that's when Deeplinks came to mind. I used the Deeplinks plugin 的 Cordova 插件,并将我的访问令牌作为参数传递。当用户登录时,BrowserTab 使用 URL 中的访问令牌重定向到我的应用程序的自定义 URL 方案 link。现在它完美地工作。我在某处读到 IOS 正在摆脱 Custom URL Scheme links。幸运的是,它仍然支持通用应用程序链接