Andrew Doig
Andrew Doig

Reputation: 25

How do I read from /Library/Application Support/ folder?

I am making a OX Cocoa app and I want to be able to read and write text files using the app on button press. These text files should be saved in /Library/Application Support/AppName but i can't get my app to reading anything from there. It can write to the folder, but not read what it has written there, even though I can see the file sitting there in finder.

Here is the code I am using the successfully write to the folder.

    NSString *text = editor.string;
    NSString *path = @"/Library/Application Support/";

    NSMutableString *mu = [[NSMutableString stringWithString:path] init];
    [mu insertString:FileName.stringValue atIndex:mu.length];
    [mu insertString:@".txt" atIndex:mu.length];

    path = [mu copy];
    [text writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];

Here is the code I am using (and failing) to read from the text files.

    NSArray *path = [[NSBundle mainBundle] pathsForResourcesOfType:@"txt" inDirectory:@"/Library/Application Support/"];
    NSString *output = @"";

    NSMutableString *mu = [[NSMutableString stringWithString:output] init];

    for (int i = 0; i < [path count]; i++) {
        NSString *text = [NSString stringWithContentsOfFile:path[i] encoding:NSUTF8StringEncoding error:NULL];
        [mu insertString:text atIndex:mu.length];
        [mu insertString:@"\n" atIndex:mu.length];
    }

    [textView setString:mu];

Any tips on what I can correct would be super helpful, I'm a bit stuck here.

Edit: Using your input I have updated my code to this:

    NSString *fileLocation = @"~/Library/Application Support/";
    NSArray *text = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fileLocation error:nil];
    NSString *output = @"";
    NSMutableString *mu = [[NSMutableString stringWithString:output] init];

    for (int i = 0; i < [text count]; i++) {
        [mu insertString:text[i] atIndex:mu.length];
        [mu insertString:@"\n" atIndex:mu.length];
    }
    [textView setString:mu];

However the text from the files is still not appearing.

Upvotes: 1

Views: 3484

Answers (3)

Peter Hosey
Peter Hosey

Reputation: 96333

Most hard-coded paths will fail when you sandbox your app. Even if you get away with this one, or you don't plan to sandbox this app, it's a bad habit that's worth getting out of.

Moreover, are you sure you want /Library and not ~/Library? The former is often not writable by the user. The latter is in the user's Home directory (or your container when sandboxed).

To get the Application Support directory, or the Caches directory, or any other directory that you may want to create things in and later retrieve them from, ask a file manager for it.

Upvotes: 2

Tyler Bindon
Tyler Bindon

Reputation: 825

/Library/Application Support is not in your bundle. The paths you get using [[NSBundle mainBundle] pathsForResourcesOfType:…] are only useful for accessing files inside your application itself (images, sounds, etc that you included when you built the app).

You want to use [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:error] to get a list of files in a directory outside your application.

Matt Gallagher has a great example of a fault-tolerant method of locating the path to your application support directory at Cocoa With Love. I would recommend using it over hardcoding the /Library/Application Support path.

NSError *error = nil;
NSArray *text = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fileLocation error:&error];
if (!text) {
    NSLog( @"Error reading contents of application support folder at %@.\n%@", applicationSupportFolder, [error userInfo] );
}

Upvotes: 1

eofster
eofster

Reputation: 2047

You're trying to get the path using NSBundle from the main bundle of the app. But the file is not in the bundle, you should specify the path manually. You could hardcode the path, store previously written paths somewhere, or use NSFileManager to get directory contents and analyze it. For example, -[NSFileManager contentsOfDirectoryAtPath:error:].

Upvotes: 0

Related Questions