简体   繁体   English

React-native:如何从 ios 项目文件夹中复制文件?

[英]React-native: How to copy file from ios project folder?

I have files in assets folder which is created in Xcode under my_project/assets.我在资产文件夹中有文件,这些文件是在 my_project/assets 下的 Xcode 中创建的。 I can normally access file in my template WebView with:我通常可以通过以下方式访问我的模板 WebView 中的文件:

source={uri:'ICF-Package/ICFPackage/index.html'} source={uri:'ICF-Package/ICFPackage/index.html'}

but I can't copy this file to LibraryDirectoryPath with react-native-fs:但我无法使用 react-native-fs 将此文件复制到 LibraryDirectoryPath:

RNFS.copyFile('.ICF-Package/ICFPackage/index.html', RNFS.LibraryaDirectory + '/index.html').then((result) => console.log('DONE')).catch((error) => console.log(error, 'ERROR')); RNFS.copyFile('.ICF-Package/ICFPackage/index.html', RNFS.LibraryaDirectory + '/index.html').then((result) => console.log('DONE')).catch((error ) => console.log(error, 'ERROR'));

Please help me with this.请帮我解决一下这个。

the file copy is under in the ios asset files.文件副本位于 ios 资产文件中。 but you pass the sourcePath to the method is relative to the react-native path.但是您将 sourcePath 传递给该方法是相对于 react-native 路径的。 it means the same folder as the component that requires it.它意味着与需要它的组件相同的文件夹。
one way, you move the file into the react-native directory, then use it.一种方法是将文件移动到 react-native 目录中,然后使用它。 It also has an advantage, for android, it also works.它还有一个优势,对于android,它也可以。
the other way, you put the file for different platforms in different files.另一种方式,您将不同平台的文件放在不同的文件中。 For android, put it in the assets, for ios put it in the RNFS.MainBundlePath.对于 android,将其放在 assets 中,对于 ios,将其放在 RNFS.MainBundlePath 中。 The asset destination and the main.jsbundle have to be in the same folder, RNFS.MainBundlePath is the main.jsbundle path.资产目标和 main.jsbundle 必须在同一个文件夹中,RNFS.MainBundlePath 是 main.jsbundle 路径。

//for android
if(Platform.OS == "android"){
RNFS.copyFileAssets("file path under the assets",destPath)
} else {
//for ios
RNFS.copyFile(RNFS.MainBundlePath + "the path under ios assets",destPath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM