Moh*_*har 55 android ios react-native
我是反应原生的新手,我想在默认浏览器中打开网址,例如Android和iPhone中的Chrome.
我们通过Android中的意图打开网址,就像我想要实现的功能一样.
我有很多次搜索,但它会给我Deepklinking的结果.
RRi*_*esh 142
你应该用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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是您可以尝试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',
},
});
Run Code Online (Sandbox Code Playgroud)
CLU*_*HER 13
一种更简单的方法,无需检查应用程序是否可以打开 url。
loadInBrowser = () => {
Linking.openURL(this.state.url).catch(err => console.error("Couldn't load page", err));
};
Run Code Online (Sandbox Code Playgroud)
用一个按钮调用它。
<Button title="Open in Browser" onPress={this.loadInBrowser} />
Run Code Online (Sandbox Code Playgroud)
小智 11
尝试这个:
import React, { useCallback } from "react";
import { Linking } from "react-native";
OpenWEB = () => {
Linking.openURL(url);
};
const App = () => {
return <View onPress={() => OpenWeb}>OPEN YOUR WEB</View>;
};
Run Code Online (Sandbox Code Playgroud)
希望这能解决您的问题。
wp-*_*com 10
在 React 16.8+ 中,以下内容可用于创建ExternalLinkBtn
组件,类似于a
网络上的标签。该链接将在默认浏览器中从外部打开。
import React from 'react';
import { Button, Linking } from 'react-native';
const ExternalLinkBtn = (props) => {
return <Button
title={props.title}
onPress={() => {
Linking.openURL(props.url)
.catch(err => {
console.error("Failed opening page because: ", err)
alert('Failed to open page')
})}}
/>
}
Run Code Online (Sandbox Code Playgroud)
ExternalLinkBtn
下面是在组件中使用 our 的示例
export default function exampleUse() {
return (
<View>
<ExternalLinkBtn title="Example Link" url="https://example.com" />
</View>
)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
61706 次 |
最近记录: |