文件下载在 IOS 的本机反应中不起作用



我正在使用"react-native-fetch-blob"版本0.10.8在react-native中下载文件。这在安卓上按预期工作。我能够按预期下载文件。至于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);
});

}

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

嗨,已经得到了这个答案,但忘了在这里回答。

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

请使用以下代码:

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

这是由于应用传输安全性。您的下载链接位于不安全的http protocaol上。你必须在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>

最新更新