How to access local files within project
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
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.
- How to organize C source file previously compiled by GCC Make and build them into an Xcode bundle? I have a Duplicate Symbol _main Error
- Lion 10.7.3: Apple is not currently accepting applications built with this version of the OS
- Asset Catalogs in resource bundles
- How to make an iOS asset bundle?
- Objective C: Reading text files
- Copy Folder (w/contents) from bundle to Documents directory - iOS
3 Solutions Collect From Internet About “How to access local files within project”
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. 🙂
NSBundle class for that. For Example:
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"bin"]; NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:path]; ...
Indeed calling [is open] is needed even though you “initialiseWithFileAtPath”. I would think the initialisation would automatically open the file for you, but it doesn’t.
- UITableView: How to change cell height dynamically when a button is clicked in it? Swift
- Seek to a certain position in AVPlayer right after the asset was loaded
- iOS – How to make an animation track touches
- Set Action Listener Programmatically in Swift
- ObjC protocol Implementation in Swift
- iOS NSUserDefaults Watching the change of values for a single key
- How to apply CIPhotoEffectMono filter on image in iOS?
- How to convert HEX to NSString in Objective-C?
- iOS – How to set a UISwitch programmatically
- Convert Apple Emoji (String) to UIImage
- How to set current playback duration and elapsed time on iOS 7 lockscreen?
- Cannot debug in Xcode: Debug info from this module will be unavailable in the debugger
- Is there a way to prevent the keyboard from dismissing?
- Converting Swift 2.3 to Swift 3.0 – Error, Cannot invoke 'dataTask' with an argument list of type'
- NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) iOS