使用expo-sharing分享文本字符串

问题描述 投票:0回答:1

我正在尝试使用“共享”对话框在 Expo 上共享一段文本。虽然从当前的文档我找不到任何与此相关的内容。

现在,我的解决方法是将文本以 .txt 格式保存到用户的设备上并共享。但肯定有更好的方法——甚至 iOS 也有“原生方法”。但是我如何实现与 Expo 的 expo-sharing 包类似的功能呢?

我当前的解决方法(为简洁起见进行了修改):

import * as Sharing from 'expo-sharing'; import * as FileSystem from 'expo-file-system'; ... const shareText = async (text) => { if (!await Sharing.isAvailableAsync()) { alert("Sharing is not available on this platform"); return; } const fileName = FileSystem.documentDirectory + "text"+new Date().getDate()+"-"+new Date().getMonth()+"-"+new Date().getFullYear()+"_"+new Date().getHours()+"-"+new Date().getMinutes()+".txt"; await FileSystem.writeAsStringAsync(fileName, text); try { await Sharing.shareAsync(fileName); } catch (error) { console.error("Failed sharing: ", error.message); } };


android ios react-native expo file-sharing
1个回答
0
投票
share

。在世博会上工作得很好。

import { Share } from 'react-native';

...

const shareText = async (text) => {
    try {
        await Share.share({
            message: text
        });
    } catch (error) {
        console.error('Error sharing:', error);
    }
};

© www.soinside.com 2019 - 2024. All rights reserved.