从本机iOS代码读取项目目录中的文件?

时间:2023-02-06 09:16:56

I have a react-native application that has some settings in a file called settings.json. I would like to read one of the settings in the file from my native iOS code. I tried the code below:

我有一个react-native应用程序,在一个名为settings.json的文件中有一些设置。我想从我的原生iOS代码中读取文件中的一个设置。我试过下面的代码:

NSFileManager *filemgr;
NSString *currentpath;
filemgr = [NSFileManager defaultManager];
currentpath = [filemgr currentDirectoryPath];
NSLog(@"Value of filePath = %@", currentpath);

But this logs the path / every time. How can I get the path for my actual project which is something like this: /Users/currentUser/Documents/development/ReactProject

但这会记录路径/每次。如何获得我的实际项目的路径,如下所示:/ Users / currentUser / Documents / development / ReactProject

I obviously can't hardcode this into my app. How can I get the directory for the current react-native project?

我显然无法将其硬编码到我的应用程序中。如何获取当前react-native项目的目录?

2 个解决方案

#1


2  

You don't really want it from your project path either as that's not available to your app under normal circumstances (it's in a sandbox)

你并不是真的想要它来自你的项目路径,因为在正常情况下你的应用程序无法使用它(它在沙盒中)

The most likely thing I think you want to do is

我认为你最想做的事情是

  1. Put the file into your app bundle (just make sure that it's in an Xcode group and assigned to your app target).
  2. 将文件放入您的应用程序包中(只需确保它在Xcode组中并分配给您的应用程序目标)。

Now it will be deployed inside of your app's bundle on whatever device it's on. It's read-only here.

现在,它将部署在应用程序包中的任何设备上。这是只读的。

  1. Read it using this:

    阅读它:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"json"]; 
    

#2


0  

I'm not really into React-Native applications but this seems to me an iOS project related problem. The logged / refers to the application folder in this case /Users/currentUser/Documents/development/ReactProject. So try to read the file as a string with:

我不是真正进入React-Native应用程序,但在我看来这是一个与iOS项目相关的问题。在这种情况下记录/引用应用程序文件夹/ Users / currentUser / Documents / development / ReactProject。因此,尝试将文件作为字符串读取:

NSString *settings = [[NSBundle mainBundle] pathForResource:@"settings"   ofType:@"json"];
NSLog(@"Content of settings: %@", settings);

Make also sure that the file is added to your project and link it to list of files for your target by selecting the right target in the "Target membership" in property of the file as in image.

还要确保将文件添加到项目中,并通过选择文件属性中“目标成员资格”中的正确目标(如图像中)将其链接到目标文件列表。

Hope this helps.

希望这可以帮助。

从本机iOS代码读取项目目录中的文件?

#1


2  

You don't really want it from your project path either as that's not available to your app under normal circumstances (it's in a sandbox)

你并不是真的想要它来自你的项目路径,因为在正常情况下你的应用程序无法使用它(它在沙盒中)

The most likely thing I think you want to do is

我认为你最想做的事情是

  1. Put the file into your app bundle (just make sure that it's in an Xcode group and assigned to your app target).
  2. 将文件放入您的应用程序包中(只需确保它在Xcode组中并分配给您的应用程序目标)。

Now it will be deployed inside of your app's bundle on whatever device it's on. It's read-only here.

现在,它将部署在应用程序包中的任何设备上。这是只读的。

  1. Read it using this:

    阅读它:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"json"]; 
    

#2


0  

I'm not really into React-Native applications but this seems to me an iOS project related problem. The logged / refers to the application folder in this case /Users/currentUser/Documents/development/ReactProject. So try to read the file as a string with:

我不是真正进入React-Native应用程序,但在我看来这是一个与iOS项目相关的问题。在这种情况下记录/引用应用程序文件夹/ Users / currentUser / Documents / development / ReactProject。因此,尝试将文件作为字符串读取:

NSString *settings = [[NSBundle mainBundle] pathForResource:@"settings"   ofType:@"json"];
NSLog(@"Content of settings: %@", settings);

Make also sure that the file is added to your project and link it to list of files for your target by selecting the right target in the "Target membership" in property of the file as in image.

还要确保将文件添加到项目中,并通过选择文件属性中“目标成员资格”中的正确目标(如图像中)将其链接到目标文件列表。

Hope this helps.

希望这可以帮助。

从本机iOS代码读取项目目录中的文件?