2

the below shown functions are working well in Xcode, as long as I use them in OSX-applications... now I wrote an iOS-application where I would like to save some data in .txt files and read them again, but it does not work... there are neither files written, nor read... I already searched the net for solutions but I only found solutions to whenever it does not work in the OSX application (wrong directory, etc...) but that is not my case, there I already got it working...

what is the difference when I use it in the iOS simulator and how to get it running there?

//the below code is now the corrected version (before it was posted without the "this was missing" part.

void SaveData(std::string FileName)
{
    //!!this was missing
        char *H = getenv("HOME");
        std::string Home(H);
        std::string FileName = Home + "/Documents/" + FileName;

    std::ofstream FileOut (FileName);

    if (FileOut.is_open())
    {
        //!!write data to file
        FileOut.close();
    }
}


void LoadData(std::string FileName)
{
    //!!this was missing
        char *H = getenv("HOME");
        std::string Home(H);
        std::string FileName = Home + "/Documents/" + FileName;

    std::ifstream FileIn;
    FileIn.open(FileName.c_str(), std::ios::in);

    if(not FileIn.good())
    {
        //!!error message
        return;
    }

    //!!read data from file
    FileIn.close();
}
  • What does "encryption process is needed for each line before writing to file" mean? Data does not have "lines", it is just bytes. Lines are an application level concept. – zaph Mar 04 '16 at 04:40
  • What is the file path, add that to the question. iOS restricts the area that can be read and written to. – zaph Mar 04 '16 at 04:44
  • "encryption process" is just a comment for me that I need to insert the data conversion functions later on, as soon as the initial test are done. its irrelevant for now (just a comment), I forgot to delete that before posting (I stripped down the code to the relevant lines only so it is more readable for you guys). the file path is "users/me/documents/xcodeprojects" thus the user defined path where I store my xcode projects... I expected something like this to be the problem, but I do not find any infos on the net where it should be (iOS vs OSX), how to set it up (or even where to set it up). – Moritz Buchty Mar 04 '16 at 11:30

2 Answers2

2

iOS is Sandboxed and restricts the area that can be read and written to and users/me/documents/xcodeprojects is incorrect.

See About the iOS File System for details.

There are several directories you can write and the Documents directory is probably what you want. You have to make a call to get the path, here are example calls in C++, Objective-C and Swift:

C++ (Thx: Moritz Buchty):

char *H = getenv("HOME");
std::string Home(H);
std::string FileName = Home + "/Documents/" + FileName;

ObjC:

NSArray *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

Swift:

let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
zaph
  • 111,848
  • 21
  • 189
  • 228
  • that c++ syntax sounds promising as with that I don't even have to read the directory out somewhere in my ObjC user Interface and route it down, I can read it directly in the c++ core program where I need it. – Moritz Buchty Mar 04 '16 at 19:19
  • it worked... with a small adjustment... the constant char "/Docs" could not be appended to the char *home, a constructor was required in-between... I inserted the new code in my example above with "this was missing" – Moritz Buchty Mar 04 '16 at 22:09
0

You need to save the file to your application's Documents folder. For more information about it, read this answer.

Community
  • 1
  • 1
0xDEADBEEF
  • 83
  • 5
  • thanks, now it all starts to make sense... I was googling for several weeks now but probably was using misleading keywords... it drove me nuts... https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW28 – Moritz Buchty Mar 04 '16 at 12:19