在默认的网络浏览器中打开网址 [英] Open Url in default web browser

查看:170
本文介绍了在默认的网络浏览器中打开网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new in react-native and i want to open url in default browser like Chrome in Android and iPhone both.

我们通过Android中的意图打开网址,就像我要实现的功能一样.

We open url via intent in Android same like functionality i want to achieve.

我搜索了很多次,但是会得到 Deepklinking 的结果.

I have search many times but it will give me the result of 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快餐:

Here's an example you can try on 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',
  },
});

这篇关于在默认的网络浏览器中打开网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆