跳到主要内容
新架构实战课 实操 + 基建 + 原理全维度包揽,抢先掌握 React Native 新架构精髓 立即查看 >Version: 0.70

Linking

Linking提供了一个通用的接口来与传入和传出的 App 链接进行交互。

Every Link (URL) has a URL Scheme, some websites are prefixed with https:// or http:// and the http is the URL Scheme. Let's call it scheme for short.

In addition to https, you're likely also familiar with the mailto scheme. When you open a link with the mailto scheme, your operating system will open an installed mail application. Similarly, there are schemes for making phone calls and sending SMS. Read more about built-in URL schemes below.

Like using the mailto scheme, it's possible to link to other applications by using custom url schemes. For example, when you get a Magic Link email from Slack, the Launch Slack button is an anchor tag with an href that looks something like: slack://secret/magic-login/other-secret. Like with Slack, you can tell the operating system that you want to handle a custom scheme. When the Slack app opens, it receives the URL that was used to open it. This is often referred to as deep linking. Read more about how to get the deep link into your app.

Custom URL scheme isn't the only way to open your application on mobile. You don't want to use a custom URL scheme in links in the email because then the links would be broken on desktop. Instead, you want to use a regular https links such as https://www.myapp.io/records/1234546. and on mobile you want that link open your app. Android calls it Deep Links (Universal Links - iOS).

Built-in URL Schemes

As mentioned in the introduction, there are some URL schemes for core functionality that exist on every platform. The following is a non-exhaustive list, but covers the most commonly used schemes.

Scheme说明iOSAndroid
mailtoOpen mail app, eg: mailto: support@expo.io
telOpen phone app, eg: tel:+123456789
smsOpen SMS app, eg: sms:+123456789
https / httpOpen web browser app, eg: https://expo.io

基本用法

If you want to enable deep links in your app, please the below guide:

要了解更多如何在 Android 上支持深度链接的说明,请参阅Enabling Deep Links for App Content - Add Intent Filters for Your Deep Links.

如果要在现有的 MainActivity 中监听传入的 intent,那么需要在AndroidManifest.xml中将 MainActivity 的launchMode设置为singleTask。相关解释可参考<activity>文档。

<activity
android:name=".MainActivity"
android:launchMode="singleTask">

There are two ways to handle URLs that open your app.

1. If the app is already open, the app is foregrounded and a Linking 'url' event is fired

You can handle these events with Linking.addEventListener('url', callback) -- it calls callback({ url }) with the linked URL

2. If the app is not already open, it is opened and the url is passed in as the initialURL

You can handle these events with Linking.getInitialURL() -- it returns a Promise that resolves to the URL, if there is one.


示例

Open Custom Settings

发送 Intents (Android)


文档

方法

constructor()

constructor();

addEventListener()

addEventListener(type, handler);

添加一个监听 Linking 变化的事件。type 参数应填url,并提供一个处理函数。


removeEventListener()

removeEventListener(type, handler);

删除一个事件处理函数。type 参数应填url


openURL()

openURL(url);

尝试使用设备上已经安装的应用打开指定的url

你还可以使用其他类型的 URL,比如一个地理位置(形如"geo:37.484847,-122.148386"或是一个通讯录名片,只要是可以通过已安装的应用打开的即可。

本方法会返回一个Promise对象。如果用户在弹出的对话框中点击了确认或是 url 自动打开了,则 promise 成功完成。如果用户在弹出的对话框中点击取消或是没有对应应用可以处理此类型的 url,则 promise 会失败。

参数:

名称类型必需说明
urlstring要打开的 URL

如果系统不知道如何处理给定的 URL,则此方法会调用失败。如果你传入的 URL 不是一个 http 链接,则最好先通过{@code canOpenURL}方法检查一下。

对于 web 链接来说,协议头("http://", "https://")不能省略!

This method may behave differently in a simulator e.g. "tel:" links are not able to be handled in the iOS simulator as there's no access to the dialer app.


canOpenURL()

canOpenURL(url);

判断设备上是否有已经安装的应用可以处理指定的 URL。

本方法会返回一个Promise对象。当确定传入的 URL 可以被处理时,promise 就会返回,值的第一个参数是表示是否可以打开 URL。

The Promise will reject on Android if it was impossible to check if the URL can be opened or when targetting Android 11 (SDK 30) if you didn't specify the relevant intent queries in AndroidManifest.xml. Similarly on iOS, the promise will reject if you didn't add the specific scheme in the LSApplicationQueriesSchemes key inside Info.plist (see bellow).

参数:

名称类型必需说明
urlstring要打开的 URL

对于 web 链接来说,协议头("http://", "https://")不能省略!

This method has limitations on iOS 9+. From the official Apple documentation:

  • If your app is linked against an earlier version of iOS but is running in iOS 9.0 or later, you can call this method up to 50 times. After reaching that limit, subsequent calls always return false. If the user reinstalls or upgrades the app, iOS resets the limit.

对于 iOS 9 来说,你需要在Info.plist中添加LSApplicationQueriesSchemes字段,否则canOpenURL可能一直返回 false。

When targeting Android 11 (SDK 30) you must specify the intents for the schemes you want to handle in AndroidManifest.xml. A list of common intents can be found here.

For example to handle https schemes the following needs to be added to your manifest:

<manifest ...>
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https"/>
</intent>
</queries>
</manifest>

openSettings()

openSettings();

Open the Settings app and displays the app’s custom settings, if it has any.


getInitialURL()

getInitialURL();

如果应用是被一个链接调起的,则会返回相应的链接地址。否则它会返回null

如果要在 Android 上支持深度链接,请参阅http://developer.android.com/training/app-indexing/deep-linking.html#handling-intents

getInitialURL may return null while debugging is enabled. Disable the debugger to ensure it gets passed.


sendIntent()

sendIntent(action: string, extras?: Array<{key: string, value: string | number | boolean}>)

@platform android Android-Only. Launch an Android intent with extras (optional)