I am working on a application that is a sort of video recorder, but i'm facing a problem, while coredata could support keeping data on migration (install uppdate, reinstall program), applications files save to the document folder of the app are deleted. I'm using a webservice for some images, but all the video recorded by the user are lost.
Is there a way to keep application local files (in the document folder) while migrating (like relaunching the app from xcode without deleting it on the phone) ?
I'm talking about path like :
filePath NSString * #"/var/mobile/Containers/Data/Application/7FB440BB-76DD-49C6-B5DD-AA9D26A86C51/Documents/dataImage/image_1_maquette_miniature.jpg" 0x000000015c528520
EDIT :
I've found that, when i rebuild my app:
/var/mobile/Containers/Data/Application/509E8B5D-A3C4-4D90-809B-47159AC60542/Documents/dataImage/image_1_gabarit_masque.png
/var/mobile/Containers/Data/Application/660EF541-CDD0-439C-BCAB-B7FE4371799A/Documents/dataImage/image_1_gabarit_masque.png
The UUID changes so here is my code to write files:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if(![self createFolderIfNoExists:#"dataImage"]) {
fileManager = nil;
return nil;
}
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"dataImage/image_%d_%#.%#",objectID,assetTypeName,extension]];
if ([fileManager fileExistsAtPath:filePath] == YES) {
[fileManager removeItemAtPath:filePath error:nil];
NSLog(#"DELETE file exists %d",[fileManager fileExistsAtPath:filePath]);
}
if([imageData writeToFile:filePath atomically:NO] == NO) {
NSLog(#"[Error] Create File %# at %#", imageData,filePath);
fileManager = nil;
return nil;
}
Related
- (void) copyDatabaseIfNeeded
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:ABC.Sqlite];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:ABC.Sqlite];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (success)
{
NSURL *url=[NSURL URLWithString:WSURL2];
} else
{
NSLog(#"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
I need to add any code for to View Sqlite File ??
I tried by adding these in Plist: UIFileSharingEnabled, CFBundleDisplayName
How can i view my SQLIte file in iTunes ?
Referred links:
How to enable file sharing for my app?, UIFileSharingEnabled has no effect
If i enabled iTunes file sharing option in my app to backup app data in PC, will appStore rejects the app ?
You are not creating the file in the Documents directory, but the Library directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
// ^^^^^^^^^^^^^^^^^^
You want NSDocumentDirectory.
I am working with the sqlite database and everything is going well. I added 4 entries in the table, and then added textview on my xib file. Now i want that this data should be fetched from the sqlite database which is placed in the bundle of the project. But i am not getting how to call this database from the bundle to thedirectory path. Please help me out regarding this issue.
Use this code:
First call this method which will check wheter dbfile is stored in documents Directory or not. If not It will write your file to documents directory.
-(void)openDatabase {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"databasefilename.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath
error:&error];
if (!success)
NSLog(#"Failed to create writable database file with message '%#'.",
[error localizedDescription]);
}
}
- (NSString *) getDBPath {
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,
NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:#"databasefilename.sqlite"];
}
Then check in Appdelegate or any other class where you want:
if (sqlite3_open([[self getDBPath] UTF8String], &YOUR_SQLITE3_OBJECT) == SQLITE_OK) {
NSLog(#"database opened successfully");
}
I have a app which takes a movie and saves it to the apps temp folder after the video is completed i need to move it to video folder on the device.... How would this be accomplished using Default File manager ?
Source code i found.... and if i save the file in the documents directory am will i be able to access it from Video app on iPhone?
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:#"txtFile.txt"];
if ([fileManager fileExistsAtPath:txtPath] == NO) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"txtFile" ofType:#"txt"];
[fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
}
NSDocumentDirectory is sandboxed for every iOS app. The API you are looking for is UISaveVideoAtPathToSavedPhotosAlbum.
For example:
UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil);
I'm trying to move all of the documents that reside in the NSDocumentDirectory to the NSCachesDirectory, and then delete all of those NSDocument files upon the first app launch of my next version update to my app. The only reason I'm doing this is to abide by Apple's iOS data storage guidelines, as my app update was rejected for saving .PDF files to the NSDocumentsDirectory (I have no clue why, because the app has never been rejected for this until this update (app is almost 2 years old, and I have been saving .PDFs this way since day 1), maybe I got a strict reviewer?)
I need to move all of the user's already downloaded .PDF files to the NSCachesDirectory and then delete them from the NSDocumentDirectory.
Is there a simple way this can be accomplished?
Thank you in advance for any help provided.
I tried this:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSUserDefaults *Defaults;
int launchCount;
Defaults = [NSUserDefaults standardUserDefaults];
launchCount = [Defaults integerForKey:#"launchCount" ] + 1;
[Defaults setInteger:launchCount forKey:#"launchCount"];
[Defaults synchronize];
if(launchCount == 1) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *dest = [path2 objectAtIndex:0];
NSArray *Contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error];
for(NSString *source in Contents)
{
if(![fileManager moveItemAtPath:source
toPath:dest
error:&error])
{
//TODO: Handle error
NSLog(#"Error: %#", error);
}
}
}
But I'm getting this error in the console:
NSFilePath=Test.pdf, NSDestinationFilePath=/Users/Charley/Library/Application Support/iPhone Simulator/5.1/Applications/96EB01D1-81B7-4ECE-B337-D2D663969EE3/Library/Caches, NSUnderlyingError=0xb566d50 "The operation couldn’t be completed. File exists"
Yes, use NSFileManager:
Get your documents path, and your caches path. Iterate over all the file, and move them using NSFileManager
- (void) moveAllDocs {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSString *sourceDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *destinationDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:sourceDirectory error:&error];
for(NSString *sourceFileName in contents) {
NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:sourceFileName];
NSString *destFile = [destinationDirectory stringByAppendingPathComponent:sourceFileName];
if(![fileManager moveItemAtPath:sourceFile toPath:destFile error:&error]) {
NSLog(#"Error: %#", error);
}
}
}
I want to delete an image from my iPhone app.
I use the method below, passing the name of the image as an argument.
The problem is that the image isn't deleted.
- (void)removeImage:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"%#.png", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(#"image removed: %#", fullPath);
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSLog(#"Directory Contents:\n%#", [fileManager directoryContentsAtPath: appFolderPath]);
}
The last two lines show the content in my app directory and the image I want to delete is still there. What am I doing wrong?
You are trying to delete a file in the Documents directory. You then read the contents of the bundle resources directory. These are not the same directory.
If you're trying to delete a file in the Documents directory, the you should rad that directory in your NSLog() at the end. If you're trying to delete a file inside your bundle, this is impossible. App bundles are signed and cannot be modified.
your code looks ok, so try adding some 'NSError' object to you code:
- (void)removeImage:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"%#.png", fileName]];
NSError *error = nil;
if(![fileManager removeItemAtPath: fullPath error:&error]) {
NSLog(#"Delete failed:%#", error);
} else {
NSLog(#"image removed: %#", fullPath);
}
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSLog(#"Directory Contents:\n%#", [fileManager directoryContentsAtPath: appFolderPath]);
}
In the code above I passed a NSError the error parameter of removeItemAtPath. If the system can't delete the file, this method will return NO and fill the error object with the error raised.
Based on your comment I found out that you are trying to delete the default.png and replace it with another one. Unfortunately, this is impossible. The image default.png is a part of your application bundle, which cannot be modified once it has been created and signed (this is a security measure from Apple, so applications cannot change after they have been reviewed). The only locations where you can create and delete files is inside the sandbox given to your application (the Documents folder).