I have a problem with the sqlite database. In my app i read an external db that I copy inside the iphone. It works!! Now I modified the database and I Have to replace it inside the iphone. I deleted the database from project and i put the new one (same name), then I start the simulator (or the iphone) and the app use again the old database.
This is the code i use for copy the database:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appDBPath = [documentsDirectory stringByAppendingPathComponent:#"On4h.db"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"On4h.db"];
success = [fileManager fileExistsAtPath:appDBPath];
if (success) {
return;
}
success = [fileManager copyItemAtPath:defaultDBPath toPath:appDBPath error:&error];
if (!success) {
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
How it's possible to replace the database with another with the same name? How its possible that now the iphone read again the old one?
Thanks!!
To change the database you need to do the below..
Open Preferences
Go to location Tab and click on the arrow indicating.
Now go through the following :
Application Support -> iPhone Simulator -> 7.1 (your simulator version) -> Applications -> Find out your project -> Library -> Caches
on the above location you will found your old database. Now replace it with new one.
NOTE: Above process is for the iPhone/iPad simulator only. For iPhone/ iPad devices you need to delete and reinstall the app.
I am trying to create my own plist file to be used in my application, I am using apple documents as refrence for doing this.
However, I have read that if you create your own plist you need to place it in the resource folder in your applications build. The issue with that is that I have no resources folder in my build... so I am woundering what should I do?.. I have read this guys answere here, he says its fine to just place the plist file in the supporting Files folder.. is this okay to do in regards to allowing the plist to be read and written too?
To read and write to plist the best practice is to copy it to document root if you want to access it through bundle you don't have write permission. I have provided a snap shot of the code here and how you can accomplish this.
NSError *err;
NSFileManager *fileManager = [NSFileManager defaultManager];
//getting the path to document directory for the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [paths objectAtIndex:0];
NSString *path = [documentDir stringByAppendingPathComponent:#"YourFile.plist"];
//checking to see of the file already exist
if(![fileManager fileExistsAtPath:path])
{
//if doesnt exist get the the file path from bindle
NSString *correctPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"YourFile.plist"];
//copy the file from bundle to document dir
[fileManager copyItemAtPath:correctPath toPath:path error:&err];
}
You can put that plist file anywhere you want.
The important thing will be copying it into the bundle. So to be sure for that check
project settings>build phases>copy bundle resources
You can open project settings by left-clicking on your project in the project navigator.
I'm trying to copy a bunch of files to my app's caches folder when the app is installed on the device. I tried to use the Copy Files phase to achieve this, but i don't really know which path to use for its destination.
is this the right approach ... ?
or is this not possible at all?
I am not sure if it is impossible using this approach. However you can always write a method which preloads your resources on launch to Caches dir. Here's sample code:
NSString* path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
path=[path stringByAppendingString:#"/plpart1.txt"];
NSError* error;
NSString* source=[[NSBundle mainBundle] pathForResource:#"pl_part1" ofType:#"txt"];
[[NSFileManager defaultManager] copyItemAtPath:source toPath:path error:&error];
I want to search each and every pdf files on whole iphone.Is it possible in iphone sdk?
I have tried following code but it is giving our own application's files.
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
NSArray *onlyPDFs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self ENDSWITH '.pdf'"]];
Each application (and its associated files) are sandboxed under iOS, hence you won't be able to access files other than those produced by your own application.
i.e.: This isn't possible.
i want to write a string in a file "file.txt": this file in my project (for Iphone) is inside Resources; I try to write a string in this file but it don't work, I show my code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"file.txt"];
NSError *error;
[outputString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
I want write in this file with simulator xcode, not with device
I'm not sure if you are able to write in your bundle, but you can in your Documents directory instead as your code does. Why don't you try this?
Use the same code and you will find your file in:
/Users/YOURUSER/Library/Application Support/iPhone Simulator/IOSVERSION/Applications/APPID/Documents/file.txt
This changed with XCode 6 and iOS 6 and this got tricky.
The documents directory are now hidden within:
~/Library/Developer/CoreSimulator/Devices/<some-id>/data/Containers/Data/Application/<some-other-id>
It seems the location is changing at each application start, so it's really hard to track. There is a simple tip I borrowed from this article and that I will include for the lazy ones:
#if TARGET_IPHONE_SIMULATOR
// where are you?
NSLog(#"Documents Directory: %#", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
#endif
Add this for example to your applicationDidFinishLaunching method, should make it easier!
If you want more details, just have a look to the original article
NB: I know the question is old, but well, I found it while searching, so I think adding an updated answer may serve some of us!
The Documents directory for your app in the simulator is located at;
~/Library/Application Support/iPhone Simulator/YOUR-IOS-VERSION/Applications/UNIQUE-KEY-FOR-YOUR-APP/Documents
Stop at a breakpoint anywhere in your app and type "po NSHomeDirectory()" in the debugger. Use Finder's Go > Go To Folder feature to jump directly there.
With the example code that you have here it will not write into your Resources folder, but into the Documents folder of the Simulator, which is actually where you should write on device, but as you mention that you only want to do this on the simulator, you could use
filePath = [[NSBundle mainBundle] bundlePath] stringByAppendingPathComnponent:#"Resources"]
But, don't do this in a shipping app, it will fail.
Paste this in the terminal.
open ~/Library/Application\ Support/iPhone\ Simulator/
In gdb, stop process and paste it, then the location of Documents will be printed.
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]
po [[[NSFileManager defaultManager] URLsForDirectory:9 inDomains:1] lastObject]
If you are using Xcode 7 and higher follow this:
Documents Directory:
file:///Users/codercat/Library/Developer/CoreSimulator/Devices/YourProjectDict/data/Containers/Data/Application/(sample digit)7F53CFB3-C534-443F-B595-62619BA808F2/Documents/your file located here