1

Is it possible to create in app's home dir (Documents or Library) dir/file using C++ code? I tried system() and posix_spawn(), which work on Simulator, but fail or real device (eg. running mkdir). Core Foundation methods seem to give only read, not write possibility too.

Edit

In addition to answer below my simplified implementation of tar.gz unpacking using C++/Core Foundation here (ios.h/ios.mm)

schmidt9
  • 4,436
  • 1
  • 26
  • 32
  • Maybe [this link](http://mobile.antonio081014.com/2013/06/create-rename-delete-read-and-write.html) will help? It is objective-c I think. – aichao Aug 14 '16 at 20:57
  • @aichao yes, but I am looking for c++ solutions, not ObjC – schmidt9 Aug 14 '16 at 21:01
  • It has been a while since I developed for iOS, but can't you link to any obj-c/c++, c/c++ code in building the app? – aichao Aug 14 '16 at 21:05
  • @aichao Not sure I understand what you mean, simply google "Objective C++" – schmidt9 Aug 15 '16 at 04:55
  • [this link](http://stackoverflow.com/questions/3684112/what-is-objective-c) is what I mean. – aichao Aug 15 '16 at 13:05
  • @aichao Ok, and what now? :) – schmidt9 Aug 15 '16 at 13:12
  • Use that Objective-C code in your app. It is not going to write itself :-) – aichao Aug 15 '16 at 13:39
  • @aichao Ah, I got it. Sure I know all this solutions, I have a c++ library and thought not to mix ObjC and C++, leaving only C++, my question is if I can do things you pointed with C++, not ObjC. But now I almost sure, it is impossible, Apple does not let use shell commands like tar or mkdir in C++ code – schmidt9 Aug 15 '16 at 13:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120967/discussion-between-aichao-and-schmidt9). – aichao Aug 15 '16 at 14:01
  • Thanks! Great to see that you have the solution. – aichao Aug 17 '16 at 15:33

1 Answers1

3

You can get the home directory using the Core Foundation library's CFCopyHomeDirectoryURL() function. (This is a C function, no Objective-C required.) Using the CFURL accessors, you can pull the path from that and use it with standard C++ or C/POSIX file I/O functions. Or you can use Core Foundation for I/O directly.

You can't used posix_spawn or system or fork/exec* in iOS due to sandboxing. No shell scripts either.

pmdj
  • 22,018
  • 3
  • 52
  • 103
  • Yes, thanks to @aichao I used `mkdir` from `sys/stat.h` to create directory, and for tar unpacking I'll write custom code – schmidt9 Aug 16 '16 at 13:54
  • You may be able to use whatever open source tar handling library OS X and FreeBSD uses. – pmdj Aug 16 '16 at 14:53
  • I need only unpacking `tar.gz` that's why I do not want overhead with shipping a whole lib – schmidt9 Aug 16 '16 at 14:56
  • I don't know how big that library is, but you could probably pare it down to just extraction. Tar isn't a terribly complex format, but there are probably a few edge cases, and file parsing is always a security risk. Anyway, whatever works best in your case! – pmdj Aug 16 '16 at 15:14