File operation not working on device - ios

What could make a file operation that is working well on the simulator, to not be working on an iOS device?
When using [NSBundle mainBundle], and the file is found by FileManager, what could be the different reasons for adjacent file operations to have different outcome?
I am noticing this sometimes, and just want to get an idea of what to think about when this happens.

Seems like you are trying to read a file in your application bundle. You may get its path by code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"ext"];
//Then you can use NSFileManager to read/copy it
All files in application bundle are readonly. You may get more information from here:
http://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW8

As #Aadhira said in the comment above, the simulator stores its files inside a bunch of folders on your mac, not in some sort of simulated main bundle/docs directory sandbox.
In order to get a static file from your main bundle you must create a path starting from [NSBundle mainBundle] and add path components onto it.

Related

is their a way to bundle sqlite database file with os x application? for both to be one file

I tried sqlite FMDB but it must store database into separate file I want solution that the database file is the same executable file ie they are bundled together into one app please help
Here's how to do it: create database with a file handle for the file system produced in the traditional way (NSSearchPathForDirectoriesInDomains and stringByAppendingPathComponent: #"mydb.sql").
Sounds like you've got that done. Drag the resulting file into your app. Once in your app bundle, the way to get at the file is like this (using the filename I made up above):
NSString *fileName = [[NSBundle mainBundle] pathForResource:#"mydb" ofType:#"sql"];
// or whatever you called the file in your bundle, use ofType: as the filename's extension
That should suffice as input to the FMDB package.

How to Localize sound files

I have sounds in my app, which are appropriate for different languages.
How can i load the correct file depending on locale without checking it manually ?
You need to add the files to Xcode and then localize them in the right hand side panel:
You then have a list of localizations:
Then the bundle will pull out the correct file.
[[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3"];
If you have properly localized the files in your project, then you'll 'automatically' get the correctly localized path by calling [[NSBundle mainBundle] pathForResource:resourcePath ofType:type];

NSHomeDirectory - [ NSBundle mainbundle ]

what is the difference between nshomedirectory and [nsbundle mainbundle]
how can i copy a file to the nshomedirectory / documents file from my mac ?
which one i need to choose for store .sqlite file ?
when and why i need to choose [nsbundle mainbundle] to some things ?
i found this site http://www.ios-developer.net/iphone-ipad-programmer/development/file-saving-and-loading/where-to-store-files but it didn't told me the difference.
[NSBundle mainBundle];
taken from apple site;
in general, the main bundle corresponds to an application file package or application wrapper: a directory that bears the name of the application and is marked by a “.app” extension.
[[NSBundle mainBundle] resourcePath];
This method returns the appropriate path for modern application and framework bundles. This method may not return a path for non-standard bundle formats or for some older bundle formats.
NSHomeDirectory();
This outputs the users home directory for example /Users/someuser/
in iOS, the home directory is the application’s sandbox directory. In OS X, it is the application’s sandbox directory or the current user’s home directory (if the application is not in a sandbox)
To answer your question:
NSBundle mainbundle refers to the location of the *.app file in the filesystem where as NSHomeDirectory points to the users home directory and nothing to do with the location of your app.
I also added the [[NSBundle mainBundle] resourcePath]; option as this may be what your looking for.
have a look through NSBundle on the apple dev site for more info,
NSBundle Class reference
Sorry I cant help with your other query, ive got no experience using sqlite.

Get contents of directory from main bundle

I am including a directory full of files in my app. I looked through the NSBundle docs, but I can't seem find out how to get the URL to this directory. I'm trying to use the main bundle object.
I have a directory "defaultImages" under a directory "Resources" in xCode. I have confirmed that these are being copied to the device, how do I get the URL to them?
Specifically, the following does not work.
[[NSBundle mainBundle]:URLForResource:#"defaultImages" withExtension:#"" subdirectory:#"Resources"];
Try
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"defaultImages" ofType:#"footype"];
Apparently NSBundle will not locate a directory for you. My solution was to put the contents of the directory inside a bundle, via How to make an iOS asset bundle?. I then added that bundle to my main application bundle.

SQLite file is not found when i run on the device

I have an SQLite database file embedded into my project. On the simulator, querying the database works fine, but not on the device. My database file path when i run on the device is this (as shown on the console with an NSLog) :
The path for the database file is : /var/mobile/Applications/5914F328-148F-52E6-1AC9-38D7FF141F9B/MyApplication.app/db.sqlite
My relevant code which looks for the DB is:
NSFileManager *fileMgr=[NSFileManager defaultManager];
NSString *dbPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"db.sqlite"];
NSLog(#"The path for the database file is : %#",dbPath);//Display the whole path
I know that the path on the simulator and on the device are completely different. So how should i change my code above to find my database file relatively?
I solved my issue, for whom that struggle on the same problem, it needs me just to include my SQLite file db.sqlite into my Bundle resources. CLick your project target-->Build Phases--->Copy Bundle Resources and then hit the + button and add your SQLite file or just drag your SQLite file from project Group and files and drop it there. Hope this help :)

Resources