打开外部 URL
openURL 这个函数是一个实用工具,允许你在设备的默认浏览器或已连接的应用中打开指定的 URL。这个函数会在内部使用 React Native 的 Linking.openURL。
签名
function openURL(url: string): Promise<any>;参数
url · 必填 ·
string要打开的外部网页地址或深度链接路径。
返回值
Promise<any>返回一个在 URL 成功打开时 resolve 的 Promise。
示例
打开外部网站
import { openURL } from '@apps-in-toss/web-framework';
function Page() {
const handlePress = () => {
openURL('https://google.com');
};
return (
<button onClick={handlePress}>
打开谷歌网站
</button>
);
}import { openURL } from '@granite-js/react-native';
import { Button } from 'react-native';
function Page() {
const handlePress = () => {
openURL('https://google.com');
};
return <Button title="打开谷歌网站" onPress={handlePress} />;
}打开深度链接
参考事项
如果无法打开外部 URL(如协议错误、网络被阻断等),Promise 可能会被 reject。
在 WebView 环境中会新开浏览器标签页,而在默认应用中则会切换到外部应用或浏览器。
最后更新于
这有帮助吗?