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,
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.
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.
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.
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.
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. :-)
Use NSBundle
class for that. For Example:
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"bin"]; NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:path]; ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With