qFile doesn't open on iOS



  • I'm using QFile to read and write files on my mobile app as below.

    QFile myFile("myfile.txt");
    if (myFile.open(QIODevice::ReadOnly)) {
        //read stuff
        myFile.close();
    } else {
        qDebug() << "File open failed.";
    }
    

    This works ok in Android, but in iOS, it doesn't work. Is there something I need to put in my Info.plist file to enable this or is there some other reason why this wouldn't work on iOS?


  • Lifetime Qt Champion

    Hi,

    You should use QStandardPaths to get access to a writable folder. You are currently using a relative path which means that you are trying to write inside the application bundle which is forbidden.

    Note that you should use QStandardPaths on all platform to avoid trying to write to ready-only folders.


  • Qt Champions 2016

    @kgregory I'm storing my app datafiles here:

        // Android: HomeLocation works, iOS: not writable
        // Android: AppDataLocation works out of the box, iOS you must create the DIR first !!
        myPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
    

    attention: on iOS you must create the directory where on Android it exists already
    I'm always creating a /data folder at AppDataLocation to store my files in



  • @ekkescorner

    So will myPath (in your example) be a directory specific to my application or is it a general folder that all of the applications use?


  • Lifetime Qt Champion

    AppDataLocation is only for your application.



  • Thanks! Here's the code I have now. It appears to be working, although I don't have my bluetooth device handy right now which prevents me from fully testing it.

    QString m_dataLocation = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
        if (QDir(m_dataLocation).exists()) {
            qDebug() << "App data directory exists. " << m_dataLocation;
        } else {
            if (QDir("").mkpath(m_dataLocation)) {
                qDebug() << "Created app data directory. " << m_dataLocation;
            } else {
                qDebug() << "Failed to create app data directory. " << m_dataLocation;
            }
        }
    
    QFile myFile(m_dataLocation + "/myfile.txt");
    if (myFile.open(QIODevice::ReadOnly)) {
        //read stuff
        myFile.close();
    } else {
        qDebug() << "File open failed.";
    }
    

  • Qt Champions 2016

    @kgregory great to hear that it works for you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.