iPhone writeToFile doesn't work - iphone

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)

Related

Where to put text files for iPhone UITextView

I have multiple UITextViews with corresponding .txt files. I'm reading them with NSString's
stringWithContentsOfFile
but I don't know the path where I should put my files. If I put it to /tmp/ on my Mac, it works in Simulator, but, of course, doesn't work on the actual device. So where should I put the files, so they'll work on both Simulator and actual Device.
Add them as resources to your project and you'll be able to load them from your application bundle using path:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"txt"];
If you want to make these files editable, though, you can keep them in Documents folder in application sandbox. To get its path you can use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
So workflow in this case may be the following:
Check if required file is present in documents folder. If no - copy it there from resources.
Read data from file (from Documents folder)
Save updated data to file (in Documents folder) if needed
Place them into Documents folder under your application structure. You can get actual path to it by calling:
NSString *docsDirPath = [NSHomeDirectory() stringByAppendingPathComponent: #"Documents"];
This will work on the device as well as in the Simulator.
You'll want either NSTemporaryDirectory or NSDocumentDirectory.
An in-depth example for using UITextView with files can be found at http://servin.com/iphone/iPhone-File-IO.html

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];
}

Where to keep .plist file in iphone app

I am developing an iPhone app, in which i want use an .plist file to save some config variables.
in my xcode where to create that .plist file and how to access it???
thank you,
You would put in the resources folder. Then use something like this to load it:
NSString *file = [[NSBundle mainBundle] pathForResource:#"TwitterUsers" ofType:#"plist"];
NSArray *Props = [[NSArray alloc] initWithContentsOfFile:file];
Where TwitterUsers is the name of your plist file.
If your plist file contains keys and values you would make the second line an NSDictionary instead of NSArray.
To store the plist file in your Documents directory you will have to include plist file in your app and then on first launch copy it to Documents directory.
To get access to the file in Documents directory:
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// <Application Home>/Documents/foo.plist
NSString *fooPath =
[documentsPath stringByAppendingPathComponent:#“foo.plist”];
I would recommend keeping it in the Resources directory in the app bundle, but you can just drag it into the project window. The NSBundle method pathForResource:ofType: should give you a path, which you can pass to NSDictionary's dictionaryWithContentsOfFile:.
Edit: Sorry, full code sample (thought I'd already copied & pasted):
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyConfig" ofType:#"plist"];
NSDictionary *myConfig = [NSDictionary dictionaryWithContentsOfFile:path];
EDIT Just occured to me "config variables" might not be immutable. If that is the case, disregard this answer.
I would recommend you use NSUserDefaults to store configuration variables instead of rolling your own system.
If you are modifying a .plist inside your application bundle, you will risk invalidating the signature on the bundle, while will not end well.
If you must store and modify your own .plist, store it in the Documents directory, which is also where your application should store anything that it downloads, creates, etc. See File and Data Management and NSFileManager.
By default if you have included the plist in the project anywhere (under Resources or otherwise) XCode will copy it into the application bundle where you can get to it with the aforementioned pathForResource call. Just thought I'd mention that as you might prefer a grouping where you do not have it in resources...
you can find the example sqlite book ..... the silte data file was save into the directory 'Document' .... you will know the prcess
Why would it invalidate the bundle?
Would this plist file in the resources folder be backed up in iTunes like files in the documents folder?

accessing plists on iphone simulator

So I have started building plist files using NSCoder, but as I"m developing i want to look at them and see what data is being saved.
How do i find/access the plist files generated by my app in the simulator (and on the device when i get to that)
Places Ive looked:
application bundle
build directory
To put them into the ~/Library/Application Support/iPhone Simulator/User/Applications/ subfolder (where they would end up if they were on a real device), you could use the following piece of code in your application:
+ (NSString *)dataFilePath:(NSString *)filename {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:filename];
}
This will put the file in the /Documents/ folder of your application's sandbox.
You can then find the file back at: ~/Library/Application Support/iPhone Simulator/User/Applications/ Application GUID /Documents/
Good luck.
wow that is dumb,
found them at /
this simulator must not emulate the real phone environment well at all.
If your plist is in the Document folder then give the path as
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
else if your plist file is in the resource folder then give the path of resource folder as
[[NSBundle mainbundle] pathforresources:..........];