如何使url字符串在react原生共享中可点击



我已经创建了react原生应用程序并添加了Linking。当我在浏览器中打开url时,它运行良好testapp://params.现在我想将此链接共享到其他应用程序。所以我使用react原生共享库来共享这个。但这个库只是将字符串显示为文本,但不可点击。我如何让这个文本可以点击,这样当用户点击该链接时,它就会打开我的应用程序。

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

有谁能帮我吗?

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

IOS

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

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

如果在Xcode而不是文本编辑器中进行编辑,则创建一个名为"的键;URL类型";,在Item 0下创建一个名为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上托管它,当用户导航到那里时,它会打开你的应用程序。

Android

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上打开您的应用程序并可点击。


最新更新