Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. qFile doesn't open on iOS
Forum Updated to NodeBB v4.3 + New Features

qFile doesn't open on iOS

Scheduled Pinned Locked Moved Solved Mobile and Embedded
14 Posts 6 Posters 5.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kgregory
    wrote on last edited by kgregory
    #1

    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?

    ekkescornerE 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • K kgregory

        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?

        ekkescornerE Offline
        ekkescornerE Offline
        ekkescorner
        Qt Champions 2016
        wrote on last edited by
        #3

        @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

        ekke ... Qt Champion 2016 | 2024 ... mobile business apps
        5.15 --> 6.9 https://t1p.de/ekkeChecklist
        QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

        K J.HilkJ 2 Replies Last reply
        4
        • ekkescornerE ekkescorner

          @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

          K Offline
          K Offline
          kgregory
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            AppDataLocation is only for your application.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • K Offline
              K Offline
              kgregory
              wrote on last edited by
              #6

              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.";
              }
              
              ekkescornerE 1 Reply Last reply
              0
              • K kgregory

                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.";
                }
                
                ekkescornerE Offline
                ekkescornerE Offline
                ekkescorner
                Qt Champions 2016
                wrote on last edited by
                #7

                @kgregory great to hear that it works for you.

                ekke ... Qt Champion 2016 | 2024 ... mobile business apps
                5.15 --> 6.9 https://t1p.de/ekkeChecklist
                QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

                1 Reply Last reply
                0
                • ekkescornerE ekkescorner

                  @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

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @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 !


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    J.HilkJ 1 Reply Last reply
                    0
                    • SGaistS SGaist

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

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @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.


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      aha_1980A 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @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.

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @J.Hilk

                        You might want to look at https://wiki.qt.io/Gerrit_Introduction

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        1
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          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.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          raven-worxR 1 Reply Last reply
                          2
                          • SGaistS SGaist

                            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.

                            raven-worxR Offline
                            raven-worxR Offline
                            raven-worx
                            Moderators
                            wrote on last edited by
                            #13

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

                            It's all in the source Luke ! ^^

                            may the source be with you! :)

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            1 Reply Last reply
                            0
                            • J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14

                              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.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved