2

嗨,我们在我们的应用程序中使用以下库,它在 Android 和 iOS 中运行良好,但在华为设备中 Share.open() 不返回任何内容。

是我们用来分享一些文本的库。

import Share from "react-native-share";

const message = "Text to share";
    
Share.open({
            message,
        }).then((res) => {
                console.log(res);
                return true;
            }).catch((err) => {
                console.log("Exception: ", err);
                return false;
        });

在华为设备中,它确实打开了共享对话框,我可以选择任何应用程序来共享消息,回到我们的应用程序后,应用程序逻辑无法继续,因为承诺没有返回任何值。

我在只有 HMS 可用的华为 P40Pro 设备上进行测试。但是,即使同时具有 HMS 和 GMS 的设备也存在相同的问题。

感谢您对此的任何帮助。

4

1 回答 1

0

我建议使用不同的 React Native Share 组件,例如https://docs.expo.dev/versions/latest/react-native/share/上的 Share 组件。我正在使用 Expo,这里是实现与您的代码相同的文本共享功能的示例代码。用华为 Mate 30 Pro 和 iPhone 测试示例代码,React Native 应用分享功能运行良好。

import React from 'react';
import { Share, View, Button } from 'react-native';

export default function ShareExample() {
  const onShare = async () => {
    try {
      const result = await Share.share({
        message: 'React Native | A framework for building native apps using React',
      });
      if (result.action === Share.sharedAction) {
        if (result.activityType) {
          // shared with activity type of result.activityType
        } else {
          // shared
        }
      } else if (result.action === Share.dismissedAction) {
        // dismissed
      }
    } catch (error) {
      alert(error.message);
    }
  };
  return (
    <View style={{ marginTop: 50 }}>
      <Button onPress={onShare} title="Share" />
    </View>
  );
}
于 2021-10-22T18:57:19.360 回答