iDevGames Forums

Full Version: Migrating Application Support to Container using C++
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
Will that line of code work in a c++ file?
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?
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.
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.
Interesting. I assumed the  employee knew what he was talking about. My mistake. Wink
Reference URL's