【问题标题】:How to make url string clickable in react-native-share如何在 react-native-share 中使 url 字符串可点击
【发布时间】:2018-11-02 13:30:08
【问题描述】:

我已经创建了 react native 应用并添加了链接。当我在浏览器 testapp://params 中打开 url 时,它工作正常。 现在我希望将此链接共享给其他应用程序。所以我使用 react-native-share 库来分享这个。但是这个库只是将字符串显示为文本但不可点击。如何使此文本可点击,以便当用户单击该链接时,它将打开我的应用程序。

 let shareOptions = {
  title: "React Native",
  message: "Hola mundo",
  url: "testapp://", //Here this should be clickable
  subject: "Share Link" //  for email
};
Share.open(shareOptions);

谁能帮帮我。

【问题讨论】:

    标签: reactjs react-native hyperlink react-native-ios


    【解决方案1】:

    编辑:对造成的误解表示歉意。将您的应用配置为通过链接打开因平台而异。

    IOS

    首先将以下内容添加到您的Info.plist

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>testapp</string>
            </array>
        </dict>
    </array>
    

    如果在 Xcode 而不是文本编辑器中编辑,请在 Item 0 下创建一个名为“URL 类型”的键,创建一个名为 URL Schemes 的键并在 URL Schemes 下设置 Item 0 等于字符串 testapp(或任何链接应该是)

    接下来在您的 AppDelegate.m 文件中将其添加到您上次导入的下方

    #import <React/RCTLinkingManager.h>
    
    Finally, add the following to your `AppDelegate.m` file under `@implementation AppDelegate`
    
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
      return [RCTLinkingManager application:application openURL:url
                          sourceApplication:sourceApplication annotation:annotation];
    }
    

    现在在地址栏中输入testapp:// 将打开您的应用

    很遗憾,通过 iMessage 使此链接可点击将工作。您最好的选择是使用以下脚本托管一个简单的网页:

    <script>
      window.location = "testapp://"
    </script>
    

    然后你可以在www.testapp.com 托管它,当用户导航到那里时它会打开你的应用程序。

    安卓

    intent-filter 添加到您的AndroidManifest.xml 文件中

    <intent-filter android:label="filter_react_native">
      <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" android:host="testapp" /> // A
    </intent-filter>
    

    就是这样!现在http://testapp 将在 Android 上打开您的应用并可以点击。


    【讨论】:

    • 感谢您的回复。我如何在 react-native-share 库中添加你的代码 let shareOptions = { title: "React Native", message: "Hola mundo", url: "testapp://", subject: "Share Link" // for电子邮件};
    • 我想让 url 在 react native 共享库中可以点击
    • 啊,好吧,您想为您的应用设置外部链接吗?我会改变我的答案
    • 我已经修改了问题以更清楚地理解。请检查,谢谢
    • 好的,我修改了答案以显示配置步骤以设置外部链接到您的应用
    猜你喜欢
    • 2015-12-20
    • 2020-02-20
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多