What is equivalent to NSHomeDirectory () in CoreFoundation?

I have a C library that I modify as little as possible to add a function and run it for iOS. It works fine on iOS 5.1, but breaks down on iOS 6 because it needs a write to write a small temporary file, and I think there is a w / Entitlements problem with where it was used for recording (/tmp/some.file).

I know that NSHomeDirectory () will provide me with the root of the sandbox from objectC, but for this you need objectC / Foundation to start. How can I get root sandboxes using only C / CoreFoundation calls?

+6
source share
2 answers

the equivalent of CoreFoundation NSHomeDirectory() is equal to CFCopyHomeDirectoryURL() . It is available with iOS 5, and the only place it is β€œdocumented” is in iOS 4.3 prior to iOS 5.0 API Differences .

If you need a temporary directory without hard-coding the tmp string, you can use confstr with the constant _CS_DARWIN_USER_TEMP_DIR and return to the TMPDIR environment TMPDIR if the confstr call fails:

 char tmpdir[PATH_MAX]; size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, tmpdir, sizeof(tmpdir)); if ((n <= 0) || (n >= sizeof(tmpdir))) strlcpy(tmpdir, getenv("TMPDIR"), sizeof(tmpdir)); CFURLRef tmp = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)tmpdir, strlen(tmpdir), true); 
+8
source

There is no home directory in iOS.

Your application is in the sandbox, so you do not have access to /tmp .

Instead, you have a tmp inside your application's sandbox.

You can use CFBundle to get the path to your application.
You can find the ../tmp/ folder by adding ../tmp/ .

Basically:

 CFBundleRef bundle; CFURLRef url; CFStringRef bundlePath; CFStringRef tmpRelPath; CFMutableStringRef tmpPath; bundle = CFBundleGetMainBundle(); url = CFBundleCopyBundleURL( bundle ); bundlePath = CFURLCopyFileSystemPath( url, kCFURLPOSIXPathStyle ); tmpRelPath = CFSTR( "/../tmp/" ); tmpPath = CFStringCreateMutable( kCFAllocatorDefault, CFStringGetLength( bundlePath ) + CFStringGetLength( tmpRelPath ) ); CFStringAppend( tmpPath, bundlePath ); CFStringAppend( tmpPath, tmpRelPath ); CFShow( tmpPath ); CFRelease( url ); CFRelease( bundlePath ); CFRelease( tmpPath ); 
+5
source

Source: https://habr.com/ru/post/926874/


All Articles