Philipp Schmid
Philipp Schmid

Reputation: 5828

Persist text file on iPhone or Simulator and read it from Mac

I would like to filter an existing word list (text file or web resource) using [UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:word] like this:

NSMutableArray * validWords = [[NSMutableArray alloc] initWithCapacity:[words count]];
for (NSString * word in words)
{
    if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:word])
    {
        [validWords addObject:word];
    }
    else
    {
        NSLog(@"reject word: %@", word);
    }
}

Using either my iPhone or the Xcode 4 device simulator, how can I persist the resulting NSMutableArray validWords to a location that I can access from my Mac?

Edit: This is meant to be a one-time operation, so anything that allows me to get the data off the device or simulator is fine. I've considered implementing a web service on my host machine to read/write the (large) text file, but was wondering if there is a more direct way to accomplish this.

Upvotes: 0

Views: 834

Answers (3)

leo
leo

Reputation: 7656

In case of the iOS simulator, you can conveniently write to a file on your Desktop. The app's directory is located somewhere:

/Users/me/Library/Application Support/iPhone Simulator/6.0/Applications/ABC/Library/Caches

So to write your valid words array to a plist on the Desktop, use:

NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [caches stringByAppendingPathComponent:@"/../../../../../../../../Desktop/words.plist"];
[validWords writeToFile:path atomically:NO];

Less fun, but much easier would be to just write it to a file in the Caches dir en open that in Finder. This also works on the device: Go to Xcode organizer > Devices > Applications and choose Download.

Upvotes: 2

FluffulousChimp
FluffulousChimp

Reputation: 9185

Something like this. I haven't tested - but you get the idea.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths objectAtIndex:0];
NSString *wordsPath = [paths stringByAppendingPathComponent:@"words.txt"];

FILE *filePointer = fopen([wordsPath UTF8String],"w");
[words enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    fprintf(filePointer,"%s\n",[obj UTF8String]);
}];
fclose(filePointer);

Then you should be able to browse to the app in ~/Library/Application Support/iPhone Simulator etc. words.txt should be there.

Upvotes: 0

You may want to look into iCloud to get data from your iDevice to your Mac (and back).

Upvotes: 1

Related Questions