Skip to content

Instantly share code, notes, and snippets.

@glebd
Created February 12, 2013 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glebd/4759724 to your computer and use it in GitHub Desktop.
Save glebd/4759724 to your computer and use it in GitHub Desktop.
Determine iPhoto library location with and without sandbox
+ (NSString *)libraryPath {
#ifdef APPSTORE
// Sandbox prevents us from accessing other apps' preferences.
// So we get a r/o access to ~/Library/Preferences/ and read the files directly.
//
// Need the following entitlement:
//
// <key>com.apple.security.temporary-exception.files.home-relative-path.read-only</key>
// <array>
// <string>/Library/Preferences/</string>
// </array>
// http://stackoverflow.com/a/10955994
const char *home = getpwuid(getuid())->pw_dir;
NSString *homePath = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:home length:strlen(home)];
NSString *plistPath = [homePath stringByAppendingPathComponent:@"Library/Preferences/com.apple.iPhoto.plist"];
NSDictionary *iPhotoPrefs = [NSDictionary dictionaryWithContentsOfFile:plistPath];
return [iPhotoPrefs objectForKey:@"LibraryPath"];
#else
CFStringRef appId = (__bridge CFStringRef)iPhotoBundleID;
return [(NSString *)CFBridgingRelease(CFPreferencesCopyAppValue(CFSTR("LibraryPath"), appId)) stringByExpandingTildeInPath];
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment