【问题标题】:File download not working in react native for IOS文件下载不适用于 IOS 的本机反应
【发布时间】:2018-05-16 11:04:59
【问题描述】:

我正在使用“react-native-fetch-blob”版本 0.10.8 下载 react-native 中的文件。这对android按预期工作。我能够按预期下载文件。至于ios,我无法下载文件。

下面是我正在使用的代码。

  downloadFinalBlog() {
const { config, fs } = RNFetchBlob;
let PictureDir = fs.dirs.PictureDir; 
let options = {
  fileCache: true,
  addAndroidDownloads: {
    useDownloadManager: true, 
    notification: false,
    title: "Great ! Download Success ! :",
    mime: "application/pdf",
      description: "Final Blog"
  }
};
config(options)
   .fetch("GET", "http://www.example.com/example.pdf")

  .then(res => {
     console.log(res);

  }).catch((error) => {

    console.log(error);
  });

}

谁能建议我有没有其他方法可以在 IOS 中以 react native 下载文件。

【问题讨论】:

    标签: react-native download


    【解决方案1】:

    您好,已经得到了这个问题的答案,但忘记在这里回答了。

    对于 ios,我们需要在 then 中使用 openDocument,它会提示用户将文件保存到所有可用的应用程序中。

    请使用以下代码:

    .then(resp => {
          // console.log(resp);
          if (Platform.OS === "ios") {
            RNFetchBlob.ios.openDocument(resp.data);
          }
        })
    

    【讨论】:

      【解决方案2】:

      这是由于应用程序传输安全性造成的。您的下载链接位于非安全 http 协议上。您必须在 Info.plist 中例外。即使在生产版本中,此异常也会保留。 在 project-directory/ios/project-name/Info.plist 在下面添加这些行

      <key>example.com</key>
      <dict>
        <key>NSExceptionAllowsInsecureHTTPLoads</key>
      	<true/>
      </dict>

      现在 NSAppTransportSecurity 密钥必须如下所示

      <key>NSAppTransportSecurity</key>
      <dict>
        <key>NSExceptionDomains</key>
      	<dict>
      		<key>example.com</key>
      		<dict>
      			<key>NSExceptionAllowsInsecureHTTPLoads</key>
      			<true/>
      		</dict>
          <key>localhost</key>
      		<dict>
      			<key>NSExceptionAllowsInsecureHTTPLoads</key>
      			<true/>
      		</dict>
      	</dict>
      </dict>

      【讨论】:

      • 嗨 Arun kumar 感谢您的建议。我试过了,但结果还是一样。相同的代码适用于android。对于 IOS,控制台日志告诉我下载运行良好,但找不到文件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2012-06-20
      相关资源
      最近更新 更多