Search code examples
iosnsfilemanagernsbundlegetfilesios-library

How to get file in a library iOS


I am trying to construct a library in IOS.

In my library ,I use a method -(void)loadNewData to load some data EX:test.csv.

But when I export my library and let the other app use my library to excute the loadNewData method. I found it can not be load correctly. It seems that because there is not such a file test.csv in the app.

How should I revise my code? Or where should I notice when I export the library?

Thank you for watch my question. And sorry for my poor English.

Here is the a part of code of the loadNewData:

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *Rawsurveys=[resourcePath stringByAppendingPathComponent:@"test.csv"];
NSString *RawsurveyResults;
    if([[NSFileManager defaultManager]fileExistsAtPath:Rawsurveys])
    {
        NSLog(@"find file");
        NSFileHandle *RawfileHandle=[NSFileHandle fileHandleForReadingAtPath:Rawsurveys];
        RawsurveyResults=[[NSString alloc]initWithData:[RawfileHandle availableData] encoding:NSUTF8StringEncoding];
        [RawfileHandle closeFile];

    }

Solution

  • You could probably create a bundle within your library and add you test.csv there. Then you can access your test.csv from your own bundle.

    Example of how I use images from my bundle. [UIImage imageNamed:@"myBundle.bundle/myImage"]. You could use the somewhat same for your file