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.
Related
I have set up a provisioning profile to debug my app (through Xcode) on a connected physical device.
The problem is this app requires certain supporting files. With the simulators on Mac, I simply navigate to the Documents directory for the app under the simulator directory and place the files there.
Is there a way to get these same files on to the physical device?
Place the files within your project files structure.
So they are copied to your App Bundle and will be available through the documents directory.
To add files to your iOS Project correctly:
Right click on the project icon at the top of the file list on the left.
Select Add files to <YourProjectName>
Select the folder/file you want to include and click Add.
Don't forget to select the correct target by selecting it from the given list. Refer to the screenshot please.
If your resource is not a single file but directory structure and you want all the directory tree copied, then remember to select the Added folders: Create groups
The files adding pop up window would seem like the following in XCode 6.x:
When the target is built, open up the bundle and your directory structure will exist inside fully intact. Not only that, these files may be accessible through iOS SDK as following.
So you may have to copy them to documents/library directory within the App as you may want to access them within the app.
Use the following code to do copy them.
// Check if the file has already been saved to the users phone, if not then copy it over
BOOL success;
NSString *fileName = #"test.jpg";
NSString *LIBRARY_DIR_PATH = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [LIBRARY_DIR_PATH stringByAppendingPathComponent:fileName];
NSLog(#"%#",filePath);
// Create a FileManager object, we will use this to check the status
// of the file and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the file has already been created in the users filesystem
success = [fileManager fileExistsAtPath:filePath];
// If the file already exists then return without doing anything
if(success) return;
// Else,
NSLog(#"FILE WASN'T THERE! SO GONNA COPY IT!");
// then proceed to copy the file from the application to the users filesystem
// Get the path to the files in the application package
NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
// Copy the file from the package to the users filesystem
[fileManager copyItemAtPath:filePathFromApp toPath:filePath error:nil];
Hope the above code sample is clear for you.
So whenever you want to access that file within your App, you can get reference to that file by getting it's path as follows:
NSString *sqliteDB = [LIBRARY_DIR_PATH stringByAppendingPathComponent:fileName];
Note: In any case if you require the files to be copied to the Documents directory within users app installed location, Replace the LIBRARY_DIR_PATH with the following:
NSString *DOCUMENTS_DIR_PATH = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Hope this answer is helpful to you!
Cheers!
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.
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];
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.
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.