Storing information ios SDK - ios

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.

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.

store and delete text file locally ios objective c

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.

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 embed custom resources and folders in iOS applications

I have an iOS application that embeds a native C binary bundled as a static library. This binary was initially designed to work on "regular" computers and relies on some specific folder layout, something like:
myapp
- images
- big
- thumbnails
- templates
- sounds
For the binary to work properly, I need to keep this layout as it is. I haven't done much iOS development and maybe this is a trivial question, but how can I replicate this folder layout within my iOS application (and how to get its absolute path within the application) ?
Every app has a sandbox with a set of folders that you could use. Would your app still function if they were in say the Documents or Library folder?
NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *libraryDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
There is also /tmp and /caches, but probably these aren't what you're looking for. I believe both of the above survive updates/sync.

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

Resources