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.



  • @ekkescorner said in qFile doesn't open on iOS:

    @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

    This, should really be in the documentation, right here
    I spend way to much time on this, thanks @ekkescorner !


  • Lifetime Qt Champion

    @J-Hilk Patches for the documentation are also welcomed ;-)



  • @SGaist
    :D
    sure, but honestly I wouldn't know where to start, is there a dokumentaion/guide for it?
    It's not that the docs are a wiki and just anyone con click on edit in the upper right corner.


  • Qt Champions 2017


  • Lifetime Qt Champion

    It's all in the source Luke ! ^^

    Most of the documentation is written in the sources themselves or in .doc files. Here you just need a clone of qtbase then edit qstandardpaths.cpp. When in doubt about the location of a piece of documentation I just run grep or ag on the source with one of the phrase to get the right file to edit.

    As for setting up your environment for submission, you have a detailed getting started guide here.


  • Moderators

    @SGaist said in qFile doesn't open on iOS:

    It's all in the source Luke ! ^^

    may the source be with you! :)



  • I'll like to add some additional informations, that I encountered, in case someone comes onto this thread.

    On Ios, since iOS 8.0, the absolute Path actually changes with each recompile/update.

    That means, if you store the absolute path somewhere, e.g in your settings, and you recompile your appliaction, you won't find the file again.

    This however does not mean, the file got deleted. It is still at QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)

    So instead of storing the absolute path, store the ´filename+extension´ and the enum QStandardPaths::StandardLocation and reconstruct the path from there.


Log in to reply
 

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