如何在 React Native 'Share' 函数中设置动态 url

How to set dynamic url in React Native 'Share' function

我在 React Native 的 Share 组件中引用了我的一个道具,一个 url (但以普通对象形式)。我尝试了几种不同的方法来让 Share 真正能够阅读它。 None 成功了。他们应该....有人看到我错过了什么吗?
这是代码:

onShareButtonPress() {
   const { image } = this.props.employee;
   const test = JSON.stringify(image);
     //console.log(test);
   Share.share({
      message: 'Here\'s your image:',
      url: test,
      title: 'Dealerslist Image'
   }, {
      // Android only:
      dialogTitle: 'Share Dealerslist goodness',
      // iOS only:
      excludedActivityTypes: [
       'com.apple.UIKit.activity.PostToTwitter'
     ]
   })

这也行不通:

 url: 'image',   

如果我记录 'test' 我得到 url 作为预期的字符串。但是Share还是认不出来。但是,如果我直接从控制台复制字符串并将其 hardcode/paste 作为 url,它会完美地工作。 此代码有效:

Share.share({
message: 'Here\'s your image:',
url: "https://firebasestorage.googleapis.com/v0/b/ag-equipment-
      southwest.appspot.com/o/1512760876838.4595.jpg?
      alt=media&token=1bfeba0d-d788-4774-91a7-7801c8084b30",
title: 'Dealerslist Image'

}

并且 'test' 记录完全如下:

"https://firebasestorage.googleapis.com/v0/b/ag-equipment-
southwest.appspot.com/o/1512760876838.4595.jpg?
alt=media&token=1bfeba0d-d788-4774-91a7-7801c8084b30"

所有这些我都尝试过共享到 ios 邮件。动态 link 不适用于邮件。

但是,如果我只是分享到 ios 剪贴板,我得到这个:

 Here's your image:%22https://firebasestorage....and so on

我是 Web 和移动开发的新手,所以我不完全确定预期的行为是否是复制“%22”作为可能的断行。无论如何,ios 邮件共享没有得到任何 url.

如有任何帮助,我们将不胜感激!谢谢!

您正在对字符串执行 JSON.stringify,这将在开头和结尾添加两个引号。

" = %22 这可能是额外 %22 代码的原因

onShareButtonPress() {
   const { image } = this.props.employee;
   Share.share({
      message: 'Here\'s your image:',
      url: image,
      title: 'Dealerslist Image'
   }
   .
   .
   .
)

但理想情况下,如果您不想在请求飞行期间处理意外字符,您会想尝试 encodeURIdecodeURI

URL ENCODING LIST

ENCODE URI

DECODE URI

const url = "https://google.com"

console.log(url);

console.log(JSON.stringify(url));