Showing posts with label documents. Show all posts
Showing posts with label documents. Show all posts

Friday, August 30, 2013

Downloading Content to Local iOS Documents Directory

This example shows you how to download a remote file into an iOS Documents Directory from your iOS device. This is helpful for updates, and persisting data into your application, to replace static data. This allows you to keep your data fresh and dynamic.

- (void) downloadNewContent {
    NSLog(@"Downloading New Content...");
    NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/temp"];
    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
    
    NSURL *url = [NSURL URLWithString:@"http://naep-sp2010dev.naepims.org:8889/update.zip"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    if(data) {
        stringPath = [stringPath stringByAppendingPathComponent:[url lastPathComponent]];
        [data writeToFile:stringPath atomically:YES];
    }
    

}

Purging Documents Directory in iOS

Your local sandboxed Documents directory may get stuffed with a bunch of garbage. To purge the directory and it's contents use this simple method:

- (void)purgeDocumentsDirectory
{
    NSLog(@"Purging Documents Directory...");
    NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSError *error = nil;
    for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]) {
        [[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
    }

}

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...