store and delete text file locally ios objective c - ios

I'm new to ios(android) developer. Do you have some specific file storage in the app's sandbox where i can store or delete some additional files?
I need to write to file some logs of handled exceptions and then store them to server when starting the application. After successful delievery to server, i want to delete this log file, and create new one, and do it again and again.

NSArray* paths = NSSearchPathForDirectoriesInDomains (NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString* directory = [paths objectAtIndex:0];
Also consider NSDocumentDirectory and NSCachesDirectory.

Related

iOS: Is this the proper way to setup a database location under the application support folder?

I have a sql file that needs to be stored in my App. A user on this site recommend me to store it underneath the Application Support folder. This is the path directory that I am using with my fmDatabase library.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
self.dataBasePath = [applicationSupportDirectory stringByAppendingString:#"Email.sqlite"];
self.fmDataBase = [FMDatabase databaseWithPath:self.dataBasePath];
Everything seems to be working fine. However, I am a little nervous that I may be doing something wrong. I am using the Application Support directory because I don't want the OS or an update to interfere with the Database. I would use the document directory ,but my App supports file share so the user can mess it up.
(application_home)/Library path is a place where you can create custom subdirectories for files you want backed up but not exposed to the user.
create a directory such as (application_home)/Library/Database and save files there.
for mac os x with sqlite The convention is to store it in the Application Support folder .
i.e. in ~/Library/Application Support/YourAppNam*/database.db
I think the data will be saved on the Application Support Folder in Mac platform, for iOS, the default is xxx/Library.
I don't see any dangerous element on this, because you can save to different folder as long as you can load it from right path.

Transfer file to physical iOS device

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!

IOS creates new Application ID everytime I open the app, or run it from Xcode

With the upgrade to IOS8 and Xcode 6.0.1, I have noticed that adding images, writing them to file, and displaying them is broken in my app that has been working for over 2 years. It works fine at the time of taking the photo and saving it - it displays it fine. I store the FilePath in Core Data. But as soon as I close the app and reopen, or run the simulator again, it disappears.
I have tracked the problem that every time it runs on both the device or the simulator, the documents directory string changes, with the APPLICATION ID changing, so the image is not found in the path as it can't seem to access the path of the previous APPLICATION ID that it had.
I looked for this and couldn't find any answers. Has anyone else seen this and have any suggestions for how I can get around it. Thanks so much!
Kat
Edit - I have added this code example. Every time I run my app, this is the path that changes.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"documents directory %#", documentsDirectory);
Store your relative path, here by calling the temp directory, but not the full absolute path. The App container is changed at every start, due to sandboxing safety.
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:#"MyPic"] URLByAppendingPathExtension:#"jpg"];
NSLog(#"fileURL: %#", [fileURL path]);
In the end I added the files into Core data, as they were referenced from Core Data objects.
This is a new change in iOS8 for safety enhancement, so you never should keep absolute path.
You save a file name instead of file URL. Each time when you use file URL, you make file URL from file name by your computed property.

How to get files into library directory for iphone device testing

I have tested my app on the ios simulator and using the following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dictdir = [documentsDirectory stringByAppendingPathComponent:#"dict.plist"];
NSLog(dict);
I found the path in which to save my file, dict.plist so that it can be used later by the app.
I am now testing on my device and the place where I am intending to store my .plist file is
var/mobile/Applications/......./Documents/freq.plist
First of all, I know that non-user data is meant to be saved in the library folder, so how do I change my code so that the path is leading to the ..../library/ directory?
And how do I actually put my file in this directory? Right now using the 'organizer', I managed to get the file into the app sandbox, but not in a particular folder.
The same code will get the correct path on the device and simulator, that's what it is meant for. However, if you want to copy it, you have to include it in your bundle, or download it from the internet. You can't just copy it with the finder. For copying from the bundle, use the the function – copyItemAtURL:toURL:error:. Downloading is a bit more complicated and is probably better suited for a separate question.
Just change NSDocumentDirectory to NSLibraryDirectory

Storing information ios SDK

So I have lots of application data for my application. However, This data does not necessarily need to be backed up to itunes.
Where would be the best place to store it?
You can put it in the caches directory
NSArray* cachePathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* cachePath = [cachePathArray lastObject];
or the temporary directory
NSTemporaryDirectory()
Note that there have been some concerns with iOS 5 as it sometimes clears the caches directory (which previous versions of iOS didn't, current iOS 5.0.1 in beta at the moment aims to solve this). You need to be careful when storing large files to the documents folder in iOS 5
NSArray* documentPathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentPath = [documentPathArray lastObject];
because of big iCloud sync times.

Resources