Skip to content

Instantly share code, notes, and snippets.

@yeahq
yeahq / gist:2391656
Created April 15, 2012 10:03
How to copy a file from resources to documents?
// From: http://stackoverflow.com/questions/4743317/iphone-how-to-copy-a-file-from-resources-to-documents
NSFileManager *fmngr = [[NSFileManager alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mydb.sqlite" ofType:nil];
NSError *error;
if(![fmngr copyItemAtPath:filePath toPath:[NSString stringWithFormat:@"%@/Documents/mydb.sqlite", NSHomeDirectory()] error:&error]) {
// handle the error
NSLog(@"Error creating the database: %@", [error description]);
}
[fmngr release];
@yeahq
yeahq / gist:2391659
Created April 15, 2012 10:04
Downloding zip and extracting in main bundle subdirectory at runtime
//http://stackoverflow.com/questions/5385480/iphone-downloding-zip-and-extracting-in-main-bundle-subdirectory-at-runtime
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"];
ZipArchive *zipArchive = [[ZipArchive alloc] init];
[zipArchive UnzipOpenFile:filepath Password:@"xxxxxx"];
[zipArchive UnzipFileTo:{pathToDirectory} overWrite:YES];
[zipArchive UnzipCloseFile];
[zipArchive release];
@yeahq
yeahq / gist:2391666
Created April 15, 2012 10:05
Show contents of Documents directory
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
@yeahq
yeahq / gist:2391674
Created April 15, 2012 10:07
Does File Exist
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectory"];
path = [path stringByAppendingPathComponent:@"SomeFileName"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
@yeahq
yeahq / gist:2391678
Created April 15, 2012 10:07
Delete File
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectory"];
path = [path stringByAppendingPathComponent:@"SomeFileName"];
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) //Does file exist?
{
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) //Delete it
{
NSLog(@"Delete file error: %@", error);
@yeahq
yeahq / gist:2391682
Created April 15, 2012 10:08
Delete Directory
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) //Does directory exist?
{
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) //Delete it
{
NSLog(@"Delete directory error: %@", error);
@yeahq
yeahq / gist:2391685
Created April 15, 2012 10:08
Create Directory
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) //Does directory already exist?
{
if (![[NSFileManager defaultManager] createDirectoryAtPath:path
withIntermediateDirectories:NO
attributes:nil
error:&error])
@yeahq
yeahq / gist:2391689
Created April 15, 2012 10:09
Save NSData File
NSData *file;
file = ...
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
path = [path stringByAppendingPathComponent:@"SomeFileName"];
[[NSFileManager defaultManager] createFileAtPath:path
contents:FileData
attributes:nil];
@yeahq
yeahq / gist:2391691
Created April 15, 2012 10:09
Load file
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"My Directory"];
path = [path stringByAppendingPathComponent:@"My Filename"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
//File exists
NSData *file1 = [[NSData alloc] initWithContentsOfFile:path];
if (file1)
@yeahq
yeahq / gist:2391693
Created April 15, 2012 10:10
Move File
[[NSFileManager defaultManager] moveItemAtPath:MySourcePath toPath:MyDestPath error:nil];