本文介绍了在默认 Web 浏览器中打开 Url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 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 中的意图打开 url 与我想要实现的功能相同.
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.
推荐答案
你应该使用 链接
.
You should use 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:
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',
},
});
这篇关于在默认 Web 浏览器中打开 Url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!