Application private directory on iOS with access to creation of files from Qt APIs

Viewed 1037

I am using Qt app on iOS & want to save files into Application's private directory so that other apps cannot access my files. Reading through the documentation here, /Documents directory under data container looks to be the path to save my files which are private to app.

Firstly I chose to use QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) which returned /var/mobile/Containers/Data/Application/123XX-XXXX-XXXX-XXXX/Library/Application Support/MyApp. But then I found that I could not create any files in there. Tried /Documents inside the App folder as well. But in vain.

Secondly I tried QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) which returned /var/mobile/Containers/Data/Application/123XX-XXXX-XXXX-XXXX/Documents. I am able to create & save my files here.

On iOS, which is the correct path to create files which are meant to be private to the application ?
How can I get the path using Qt?

The directory named with some ID as 123XX-XXXX-XXXX-XXXX, Is this the application directory or is it Library/Application Support/MyApp inside the same directory ?

1 Answers

I had same problem, for example I created a sqlite file. But the file that you will create can be every file you wants. In my example I need to create a sqlite file.

I solved like this:

    QString dbName = "mydb.sql";
    QString dbLocation;

       #ifdef Q_OS_ANDROID
          dbLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
       #endif

       #ifdef Q_OS_IOS
          dbLocation = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
       #endif

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(dbLocation + "/" + dbName);
    qDebug() << db.database(dbLocation + "/" + dbName);
    qDebug() << db.databaseName();

Now you can save the file, I used DocumentsLocation to get a path, always works on iOS, but not work on Android, to be ready to Android you use another path: AppDataLocation. In the documents location you can write/read/put and create files on iOS. In the AppDataLocation location you can write/read/put and create files on Android. Permission not denied in DocumentsLocation.

Related