Migrating Application Support to Container using C++

Apprentice
Posts: 6
Joined: 2012.01
Post: #1
Hello, I am in the process of trying to figure out how to move my save game files from the Application Support Directory to a Container. This is the code I have so far, but I get an error with the first line:

Code:
CFURLRef homeDirURL = CFCopyHomeDirectoryURLForUser(NULL);
CFURLRef totalsURL = CFURLCreateCopyAppendingPathComponent(NULL, homeDirURL, CFSTR("Library/Application Support/my game/Totals.txt"), 0);
        
char totalsPath[PATH_MAX] = {0};
CFURLGetFileSystemRepresentation(totalsURL, true, (uint8_t *)totalsPath, PATH_MAX);
std::ofstream write_totals(totalsPath, std::ios::out);
        
// save data and close file...

CFRelease(homeDirURL);
CFRelease(totalsURL);

Thanks.
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #2
If you look at Apple's docs on sandboxing, you'll see that the design intention is for you to use NSHomeDirectory(), which returns either the root of the sandbox or the user's actual home folder depending on whether or not your app is sandboxed.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2012.01
Post: #3
Will that line of code work in a c++ file?
Quote this message in a reply
⌘-R in Chief
Posts: 1,261
Joined: 2002.05
Post: #4
Quote: This is the code I have so far, but I get an error with the first line: ..... Thanks.

How do you expect anyone to help if you don't even tell us what the error is?
Quote this message in a reply
⌘-R in Chief
Posts: 1,261
Joined: 2002.05
Post: #5
I'm puzzled. I just came across this https://devforums.apple.com/message/714742 and see that it's you. And in that thread you suggest that it's all working fine as of two days ago, but yesterday it's not again?

At any rate, as CFM said, don't use CFCopyHomeDirectoryURLForUser; it was a mistake that he included it. (It's a private function.) Use CFURLCreateFromFileSystemRepresentation(getpwuid()) if you don't want to use Obj-C or Obj-C++. Otherwise you can use or create wrapper around NSSearchPathForDirectoriesInDomains / NSHomeDirectory / NSFileManager -URLForDirectory:NSApplicationSupportDirectory ... etc.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
getpwuid is not OK, it'll return the real home directory not the sandbox one. getenv("CF_FIXED_USER_HOME") or similar may be OK, but the NS API is the right one.
Quote this message in a reply
⌘-R in Chief
Posts: 1,261
Joined: 2002.05
Post: #7
Interesting. I assumed the  employee knew what he was talking about. My mistake. Wink
Quote this message in a reply
Post Reply