Arc*_*rma 6 deep-linking react-native firebase-dynamic-links
描述::
我正在开发反应本机应用程序。我的应用程序有登录和注册页面。注册后,我将向用户发送一封电子邮件以验证帐户。用户将获得一个验证帐户的链接,单击该链接后,我将用户重定向到我正在验证他的网址。现在,如果用户使用手机进行验证,我想将用户重定向到“我的应用程序”验证屏幕内。
使用 React Native 是如何完成的?
重定向到电子邮件验证链接后,如何将用户移动到“我的应用程序”内?
我正在使用深层链接概念来重定向应用程序内的用户。我遵循的步骤如下:
对于安卓::
我已将其添加到 AndroidManifest.xml 文件中::
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
Run Code Online (Sandbox Code Playgroud)
并添加数据如下:
<intent-filter android:label="filter_react_native">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="http"
android:host="myurl"
android:pathPrefix="/verify-email"/>
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
之后,我在我的 app.js 文件中添加了以下代码:
componentDidMount() {
console.log('hi');
Linking.addEventListener('url', this._handleOpenURL);
}
componentWillUnmount() {
Linking.removeEventListener('url', this._handleOpenURL);
}
_handleOpenURL(event) {
console.log('url', event.url);
Run Code Online (Sandbox Code Playgroud)
当我点击此网址“ http://myurl/verify-email/demo ”时,我没有在控制台中获取任何数据,也没有打开我的应用程序。我正在调试模式下运行我的应用程序。
Firebase 动态链接也可以实现此目的,但有人可以告诉我其中哪种方法最好?
对于“Firebase Dynamic Link”,我应该在哪里添加代码来处理整个应用程序的动态链接?
您无法直接将redirect_uri设置为您的移动应用程序(因为大多数身份验证提供商不支持自定义OAuth方案)。
但是您可以创建一些网页,该网页将接受来自 OAuth 提供商的重定向并打开您的应用程序(并发送所有重定向参数,例如 token )。例如,您创建页面https://example.com/oauth/并设置callback_url为https://example.com/oauth/XXXXX_provider,因此当用户将被重定向到页面时,https://example.com/oauth/XXXXX_provider&token=xxx它将使用appName://example/oauth/google?token=xxx
您可以appName://example/oauth/google?token=xxx使用Deeplink处理打开您的应用程序(当它安装在设备上时,它将打开您的移动应用程序)
处理重定向的页面示例:
<html><head></head><body>
<p>Please wait while we redirect you to Your APP NAME...</p>
<p><a href="javascript:redirectToApp()">Open appname</a></p>
<script>
var redirectToApp = function() {
var scheme = "appnameapp";
var openURL = "appname" + window.location.pathname +
window.location.search + window.location.hash;
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
var Android = /Android/.test(navigator.userAgent);
var newLocation;
if (iOS) {
newLocation = scheme + ":" + openURL;
} else if (Android) {
newLocation = "intent://" + openURL + "#Intent;scheme=" + scheme +
";package=com.appnameapp;end";
} else {
newLocation = scheme + "://" + openURL;
}
console.log(newLocation)
window.location.replace(newLocation);
} window.onload = redirectToApp;
</script>
</body></html>
Run Code Online (Sandbox Code Playgroud)
但是,WebView 默认情况下不与 Safari/Chrome 共享 cookie/会话数据。因此它对于登录流程并不理想,因为它不使用 Chrome/Safari 中现有的登录会话。
Expo 提供了一个WebBrowser api,可以打开 Safari/Chrome 而不是 webview。请注意,它会在应用程序内打开 Safari/Chrome,而不是使用链接重定向浏览器。因此,用户始终可以在浏览器中找到一个按钮来返回您的应用程序。
您可以用来WebBrowser.openAuthSessionAsync(url)打开一个安全会话,该会话与设备中的本机浏览器共享 cookie/会话信息。
Expo还提供了另一个名为AuthSession的api ,它简化了很多样板并提供了一个简单的api。
| 归档时间: |
|
| 查看次数: |
4688 次 |
| 最近记录: |