如何在 React Native 中打开应用内浏览器窗口?

新手上路,请多包涵

当我单击 URL(适用于 iOS 和 Android)时,我试图在不离开应用程序的情况下打开浏览器窗口。

行为应如下所示(以 airbnb 应用程序为例):

我怎样才能做到这一点?我需要使用任何指定的现有库吗?

我正在使用 react-native 0.37。

原文由 Facundo Bazzana 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
1 个回答

您可以 为 React Native 使用新的 InAppBrowser 插件,查看下一个示例:

 import { Linking } from 'react-native'
import InAppBrowser from 'react-native-inappbrowser-reborn';

...
  async openLink() {
    try {
      const isAvailable = await InAppBrowser.isAvailable()
      const url = 'https://www.google.com'
      if (isAvailable) {
        InAppBrowser.open(url, {
          // iOS Properties
          dismissButtonStyle: 'cancel',
          preferredBarTintColor: 'gray',
          preferredControlTintColor: 'white',
          // Android Properties
          showTitle: true,
          toolbarColor: '#6200EE',
          secondaryToolbarColor: 'black',
          enableUrlBarHiding: true,
          enableDefaultShare: true,
          forceCloseOnRedirection: true,
        }).then((result) => {
          Alert.alert(JSON.stringify(result))
        })
      } else Linking.openURL(url)
    } catch (error) {
      Alert.alert(error.message)
    }
  }
...

另一方面,您可以将此插件与深度链接一起使用,请查看项目的 README。

原文由 jdnichollsc 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题