App data loss from NSDocumentDirectory in iOS 5 - iphone

When I update from iOS 4+ to iOS 5, I lose all data in my own app. I also upgraded to OSX Lion and Xcode 4. I back up my iPhone in iTunes and was able to get the data using iPhone Backup Extractor.
I save my app data to a file in the following way:
- (NSString *)iouArrayPath
{
// Standard way of getting file path for iOS
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir = [paths objectAtIndex:0];
NSLog(#"Directory: %#", dir);
return pathInDocumentDirectory(#"IouTableArray.data");
}
- (void)archieveIou
{
NSLog(#"archieveIou");
// Get the path to the document directory
NSString *iouPath = [self iouArrayPath];
// grab the objects to archieve
NSMutableArray *iouTableArray = [iouViewController iouTableArray];
// ARCHIVE DATA
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:iouTableArray forKey:#"IouTableArray"];
[archiver finishEncoding];
[data writeToFile:iouPath atomically:YES];
if([data writeToFile:iouPath atomically:YES]) {
NSLog(#"writeToFile success!");
}
else {
NSLog(#"writeToFile failed");
}
[archiver release];
}
This is what I've tried to understand. I open the project in Xcode 4, I copy the .data file from the backup and paste it to the /iPhone Simulator/5.0/Applications/Some digit/Documents. Theorectically, when the app starts, it will look into that folder and load the .data file if it exists. It does not. However, the app will work fine if I create some test data and save it. It will replace the backup file with the same file name and properly load it when I restart the app.
This makes me wonder if there is some NSCoder/archieving change between XCode3/XCode4 or between iOS4 and iOS5.
Does anyone know what is going on? Once I figure this out, I plan to use the backup data file and copy it to the current app /Document folder. Actually, come to think of it, I am not sure how to do that either. The /Document folder for the app reside within the phone. I'll have to see if I can use iTunes and inject the app with the data. Anyways, first things first! Understand the system.
Thanks!

It turns out that iOS5 installs an older version of the app. Strange but make sure you get all your provisioning squared out.

Related

Objective C - how to access a file from iPhone/Application/Documents

I am using the following method to write image files out to the Applications/Documents folder:
- (void)saveImage:(UIImage *)image toFile:(NSString *)fileName
{
//get the data from the representation
NSData * binaryImageData = UIImagePNGRepresentation(image);
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
//write the data to a file
[binaryImageData writeToFile:[basePath stringByAppendingPathComponent:fileName] atomically:YES];
}
I thought I could just copy them to my computer, but that's not working out for me. Then, I decided to try to write the files to my hard drive. Nope. I guess that was a dumb idea, too.
I am processing bitmap images that I just want to capture somewhere (not on the device) where I can then retrieve them for testing. I've mucked with this for several hours and now it's getting late and I'm tired. Definitely ready for a little help.
Thank you in advance.
This question was answered by Rajesh Choudhary, but there were a few more hoops to go through to get the images off the phone and I wanted to post the entire process to help someone else in the future. So, here are the steps.
Enable file sharing from your app's info.plist file:
Use the code in the original post to write the file out to the
application /Documents folder.
Run your app to save the file to the iPhone application /Documents folder.
Check that your files were save by opening Organizer and selecting the iPhone running the application and selecting the application. The saved files will be located in the Documents folder in Data files in Sandbox.
To access them, open iTunes. Select the device running the application and select the Apps tab. Then, scroll to the File Sharing section, select the app and voila! The documents folder will display all your saved files on the device.
You can then select one or more of the files and click the Save to... button to save the files to your computer.
if you want to use images from device to your computer... you have to enable file sharing option in your app... When you enable it from your app's info.plist file you can see document folder in itunes.
Select you app from list and then, You can put & get files to/from document directory.
if you want to access image from document directory in app so you can use this method.
-(UIImage *)getImageFromDocumentDirectoryForFileName:(NSString *)fileName
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
UIImage *image =[UIImage imageWithContentsOfFile:[basePath stringByAppendingPathComponent:fileName]];
return image;
}

how to find a file that was created programmatically directly from my iphone

I've created an image file in an application and stored it in the fileDirectory using the following code:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
UIImage* videoImage = [self resizeImage:[UIImage imageWithData:self.videoImageData] toWidth:width andHeight:height];
NSData *imageData2 = UIImageJPEGRepresentation(videoImage, imageQuality);
[imageData2 writeToFile:[basePath stringByAppendingPathComponent:#"myfile.png"] atomically:YES];
now from my iPhone device how can i find and open this file?
In Xcode you can go to Window->Organizer and select the Devices tab.
Under the device in the panel on the left you will see Applications. Select your application to see the file system for the app sandbox. Select Download to save all the data to the mac (as a package file).
You can right-click a package file to browse as a folder by selecting Show Package Contents from the context menu.
You would need to connect your iPhone to a Mac to see it. iExplorer does the trick.
imageData2 writeToFile:[basePath stringByAppendingPathComponent:#"/myfile.png"] atomically:YES];
you missed back slash

Data storing in plist works in simulaor but not in device

I am new to iPhone development. I have created plist such as from my previous post. It works well in simulator but not in device.
I am getting the saved value from the plist and checking for the condition. When I use simulator it works but not in device.
(1) You can't write to a file in the resource folder of an iPhone. It is part of the security system of the phone that prevent malicious code from altering an application after it has been installed.
(2) If you want to save new data to a file you need to write the file to one of the automatically generated folders. Whenever an iPhone app is installed on the device or simulator, the system creates a default set of folders.
(3) You want to write to either the Preferences folder or the Documents folder. If the data concerns the operation of the app, write it to preferences. If it contains user data write to Documents.
Preferences:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);
NSString *prefsDirectory = [[sysPaths objectAtIndex:0] stringByAppendingPathComponent:#"/Preferences"];
Documents:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
NSString *docsDirectory = [sysPaths objectAtIndex:0];
Say you want to read a default preference plist file, make some changes and then save it the preferences folder.
NSString *plistPath=[[NSBundle mainBundle] pathForResource:#"PlistFileName" ofType:#"plist"];
NSMutableArray *defaultPrefs=[[NSMutableArray alloc] initWithContentsOfFile:plistPath];
//... modify defaultPrefs
NSString *outputFilePath=[prefsDirectory stringByAppendingPathComponent:#"alteredPrefs.plist"];
[defaultPrefs writeToFile:outputFilePath atomically:NO];

Importing documents in our app from iphone

i want to import some document in my app from iPhone something like PDF file or From another app ? is any way to do this ?
any link about that?
thanks
Here is a link about allowing the app to share its documents folder with itunes.
http://www.raywenderlich.com/1948/how-integrate-itunes-file-sharing-with-your-ios-app
Alternatively you could use code in your app to write files into the apps "documents" directory directly. If neither of these two things are what you are looking for you can register your app to support a file format? But that doesn't sound like what you want.
- (NSString*)DocumentsPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE);
return [paths objectAtIndex:0];
}
//not every class supports writeToFile
- (void)writeToFile:(id)object withFileName:(NSString*)filename
{
[object writeToFile:[[self DocumentsPath] stringByAppendingPathComponent:filename] atomically:TRUE];
}

iPhone writeToFile doesn't work

I have written one application that save txt file into device. When I test on simulator, I am able to read and write file. After I test on device, read is working fine but it doesn't write anything on that text file. I have searched for the solution but it seems like it has problem with right access. Anyone can suggest me how to make this work?
What path are you using to save the file to?
I'm using
- (NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:#"myFileNameHere.txt"];
}
and then pass [self dataFilePath]; into the writeToFile method as the path
On real device you don't have access to write everywhere. See "Commonly Used Directories" in Developers Guide - you should write to Documents or Caches directories (how to get their paths)