android - 在默认网络浏览器中打开 URL

标签 android ios react-native

我是 react-native 的新手,我想在 Android 和 iPhone 中的 Chrome 等默认浏览器中打开 url

我们通过 Intent 在 Android 中打开 url,就像我想要实现的功能一样。

我已经搜索了很多次,但它会给我Deepklinking的结果。

最佳答案

您应该使用 Linking .

文档中的示例:

class OpenURLButton extends React.Component {
  static propTypes = { url: React.PropTypes.string };
  handleClick = () => {
    Linking.canOpenURL(this.props.url).then(supported => {
      if (supported) {
        Linking.openURL(this.props.url);
      } else {
        console.log("Don't know how to open URI: " + this.props.url);
      }
    });
  };
  render() {
    return (
      <TouchableOpacity onPress={this.handleClick}>
        {" "}
        <View style={styles.button}>
          {" "}<Text style={styles.text}>Open {this.props.url}</Text>{" "}
        </View>
        {" "}
      </TouchableOpacity>
    );
  }
}

这是一个您可以在 Expo Snack 上尝试的示例:

import React, { Component } from 'react';
import { View, StyleSheet, Button, Linking } from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
       <Button title="Click me" onPress={ ()=>{ Linking.openURL('https://google.com')}} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  },
});

关于android - 在默认网络浏览器中打开 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804032/

相关文章:

android - 如何在 Android Studio 中使用 fork 的 git 项目

ios - 我是否需要将 Entitlements.plist 文件设置为我的目标才能使推送通知正常工作?

ios - 我无法使用 Expo 在 iOS 中测试我的应用程序

javascript - 使用 Expo 和 Typescript 创建StackNavigator,不渲染任何内容

android - 根据 Google Play 中的标准设置应用程序价格

在 Activity 之间传递时 Android Bundle 损坏了吗?

android - 在未知数量的 RecyclerView 数据更改(插入、删除、更新)时,如何在视觉上保持相同的滚动位置?

ios - 设备锁定时的文件安全和 iOS7 中的后台获取

ios - Xcode 5,禁用 Storyboard

swift - React Native - 调用 native Swift 函数调用另一个触发 RCTEventEmiiter 发送事件的函数导致 RCTCallableJSModules 错误