如何使URL字符串在react-native-share中可单击

时间:2018-11-02 13:30:08

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

我已经创建了react native应用并添加了Linking。当我在浏览器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);

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

编辑:对误解表示歉意。根据平台的不同,将应用程序配置为通过链接打开的方式也不同。

IOS

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

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

如果使用Xcode而不是文本编辑器进行编辑,请在Item 0下创建一个名为“ URL类型”的密钥,并在URL Schemes下创建一个名为Item 0的密钥。等于字符串URL Schemes(或任何链接)

在您的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]; } 将打开您的应用

不幸的是,通过iMe​​ssage使该链接可单击。最好的选择是使用以下脚本托管一个简单的网页:

testapp://

然后,您可以将其托管在<script> window.location = "testapp://" </script> 上,当用户导航到该应用时,它将打开您的应用。

Android

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> 将在android上打开您的应用,并且可以单击。