【问题标题】:react native - can't get url from share intent反应原生 - 无法从共享意图中获取 url
【发布时间】:2021-03-22 19:32:52
【问题描述】:

我希望它可以与我的 react native 应用程序共享网页。在用户与我的应用程序共享后,我需要能够提取 url。 这是我的 AndroidManifest.xml 的一部分

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <data android:scheme="https"/>
          <!-- note that the leading "/" is required for pathPrefix-->
        </intent-filter>
        <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <data android:scheme="http"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>
      </activity>

这是我试图检索 url 的本地反应代码 -

useEffect(() => {
        Linking.getInitialURL().then((url) => {
            if (url) {
              alert('got url ', url);
            }
        }).catch((e) => {
          alert('got some error ', e)
        });
      }, []);

useEffect(() => {
        const callback = (url) => alert("get url when app was open " + decodeURI(url));
        Linking.addEventListener('url', callback);
        return () => {
          Linking.removeEventListener('url', callback);
        };
      }, []);

这里有 2 个选项 - 一个当应用程序已经打开时,另一个当应用程序被共享进程打开时。 我没有在任何选项中获得网址。 我正在以非调试模式在连接到我的笔记本电脑的真实 android 设备上运行该应用程序。 而且似乎应用程序一直在不断刷新。

谁能解释我做错了什么?

谢谢

【问题讨论】:

    标签: android react-native deep-linking


    【解决方案1】:

    我认为您混淆了深层链接和共享菜单。 如果您想将 url 分享到您的应用程序,您可以使用此扩展程序 https://www.npmjs.com/package/react-native-share-menu。这会将您的应用添加为目标,同时从其他应用/浏览器共享。

    const handleShare = useCallback((item: ?SharedItem) => {
    if (!item) {
      return;
    }
    
    const { mimeType, data, extraData } = item;
    
    setSharedData(data);
    setSharedMimeType(mimeType);
    // You can receive extra data from your custom Share View
    console.log(extraData);
    }, []);
    useEffect(() => {
        ShareMenu.getInitialShare(handleShare);},
    []);
    

    您可以在“item: ?SharedItem”的“data”字段中找到您要查找的url

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2023-04-03
      • 2021-01-23
      • 1970-01-01
      • 2018-04-15
      相关资源
      最近更新 更多