Skip to content

Instantly share code, notes, and snippets.

@swillits
Created February 17, 2012 04:32
Show Gist options
  • Save swillits/1850690 to your computer and use it in GitHub Desktop.
Save swillits/1850690 to your computer and use it in GitHub Desktop.
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Create a pretend Desktop and Documents folder
NSString * desktopFolderPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Desktop"];
NSString * documentsFolderPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Documents"];
[[NSFileManager defaultManager] createDirectoryAtPath:desktopFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
[[NSFileManager defaultManager] createDirectoryAtPath:documentsFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
// URLs which we will reference
NSURL * projectURLInDesktop = [NSURL fileURLWithPath:[desktopFolderPath stringByAppendingPathComponent:@"Project"]];
NSURL * resourceURLInDesktop = [NSURL fileURLWithPath:[desktopFolderPath stringByAppendingPathComponent:@"Resource"]];
NSURL * projectURLInDocuments = [NSURL fileURLWithPath:[documentsFolderPath stringByAppendingPathComponent:@"Project"]];
NSURL * resourceURLInDocuments = [NSURL fileURLWithPath:[documentsFolderPath stringByAppendingPathComponent:@"Resource"]];
// 1) User creates a file named Resource on the desktop
[[NSData data] writeToURL:resourceURLInDesktop atomically:YES];
// 2) User uses our application to create a project file named Project on the Desktop
[[NSData data] writeToURL:projectURLInDesktop atomically:YES];
// 3) User adds a reference to Resource into the project. Project will
// contain an NSURL bookmark to Resource, and that bookmark will be
// relative to the Project file's URL on the desktop.
NSData * resourceBookmarkData = [resourceURLInDesktop bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil
relativeToURL:projectURLInDesktop error:nil];
// 4) Pretend that resourceBookmarkData is saved into the Project file
// 5) Pretend that time has passed and our application has been quit.
// 6) In Finder, let's say the user takes Project and Resource off of
// the Desktop and moves them both into the Documents folder.
[[NSFileManager defaultManager] moveItemAtURL:projectURLInDesktop toURL:projectURLInDocuments error:nil];
[[NSFileManager defaultManager] moveItemAtURL:resourceURLInDesktop toURL:resourceURLInDocuments error:nil];
// 7) Pretend that the user now creates a new file named Resource on
// the Desktop. This file is entirely unrelated to Project.
[[NSData data] writeToURL:resourceURLInDesktop atomically:YES];
// 8) The user now opened our application again, and opens the Project.
// When Project opens, it should resolve the bookmark data which
// points to Resource. Since we saved the bookmark data as relative
// to the Project file URL, when resolving the URL, it should be resolved
// relative to the current Project file URL.
NSURL * resolvedResourceURL = [NSURL URLByResolvingBookmarkData:resourceBookmarkData
options:0
relativeToURL:projectURLInDocuments
bookmarkDataIsStale:NULL
error:nil];
// 9) At this point, we expect that the resolved URL should point to
// the Resource file in the Documents folder, however it resolves
// to the Resource file in the Desktop folder.
// When the bookmark was created the relative path from Project to
// Resource was simply "../Resource". When resolving the bookmark,
// given that Project is in Documents, the resolved URL should
// be "Documents/Project" + "../Resource" = "Documents/Resource".
NSLog(@"Resolved URL file path should end in Documents/Resource.");
NSLog(@"Resolved URL file path: %@", [resolvedResourceURL relativePath]);
// --- Cleanup ---
[[NSFileManager defaultManager] removeItemAtURL:resourceURLInDesktop error:nil];
[[NSFileManager defaultManager] removeItemAtURL:projectURLInDocuments error:nil];
[[NSFileManager defaultManager] removeItemAtURL:resourceURLInDocuments error:nil];
[[NSFileManager defaultManager] removeItemAtPath:desktopFolderPath error:nil];
[[NSFileManager defaultManager] removeItemAtPath:documentsFolderPath error:nil];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment