Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access local files within project

Tags:

I want to add some static files (image, binary, etc...) to my app. I've placed them under a folder named Resources and have added it to my XCode project.

Next, I have those files added to the Copy Bundle Resources in the Build Phases tab in the project settings.

However, I can't seem to refer to them in the right way.

For instance, reading a binary file:

NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:@"data.bin"]; if(![is hasBytesAvailable]) NSLog(@"wrong"); 

This always fails & prints wrong

I tried a similar approach with NSData initWithContentsOfFile: but this wouldn't even execute. Another attempt was to use [NSBundle mainBundle] but this also didn't execute fully.

I'm new to iOS please help I can access my local files! Any help would be appreciated.

Thank you,

like image 766
chizzle Avatar asked Oct 15 '11 07:10

chizzle


People also ask

How do I access files locally?

Using Google Chrome to access local files is as easy as pressing Ctrl + O at the same time. This interface will open, allowing you to navigate to whichever file or folder is needed.

How do I view local files in HTML?

The HTML input element of type="file" allows users to select one or more files from the local file system. Before HTML5, the purpose of the file input was solely to enable users to select files to be uploaded via a form.

Can Chrome access local files?

For security reasons, by default the Chrome browser does not allow extensions to access local files. If you want the accelerator to access local files (locations of "file:///...", instead of "http://" or "https://"), you must configure Chrome to allow the access.

Can a web app access local files?

Web browsers (and JavaScript) can only access local files with user permission. To standardize the file access from the browser, the W3C published the HTML5 File API in 2014. It defines how to access and upload local files with file objects in web applications.


2 Answers

You need to ensure that you're accessing the file in the right place. Xcode places your resource files in the application's "application bundle" in the Resources directory. There are many ways to dig 'em out.

A common way is to get the pathname of a file in your bundle:

NSBundle *mainBundle = [NSBundle mainBundle]; NSString *myFile = [mainBundle pathForResource: @"data" ofType: @"bin"]; 

If you have other files in nonstandard places in your bundle, there are variations to pathForResource that let your specify a directory name.

If you want to see the actual location on your hard-drive that the simulator is using so you can inspect it, you can say:

NSLog(@"Main bundle path: %@", mainBundle); NSLog(@"myFile path: %@", myFile); 

Search for the "Bundle Programming Guide" in the Xcode documentation library to get started. :-)

like image 121
Jim Hayes Avatar answered Sep 30 '22 14:09

Jim Hayes


Use NSBundle class for that. For Example:

NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"bin"]; NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:path]; ... 
like image 41
nacho4d Avatar answered Sep 30 '22 14:09

nacho4d