Results 1 to 3 of 3

Thread: iOS AppData directory changes with each build: where to save files?

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: iOS AppData directory changes with each build: where to save files?

    Hello,

    I have written a Qt application that targets Android and iOS. Users can download content of interest from a remote server through the app. Content is saved to QStandardPaths::writableLocation(QStandardPaths:: AppDataLocation). The corresponding local URLs are saved to a database, from which models are derived for display in QML views, etc. This works great. The problem is that when I push an update, the URLs in the database no longer "work". For example:

    AppDataLocation for BundleVersion 6: /var/mobile/Containers/Data/Application/0EA7343F-5483-4D11-A3C7-0BCC088E57B4/Library/Application Support/<organization>/<app>/
    AppDataLocation for BundleVersion 7: /var/mobile/Containers/Data/Application/6912CF98-818C-4078-8BBB-FD52B77D74A3/Library/Application Support/<organization>/<app>/

    After deploying version 7 -- or even a fresh build of version 6! -- to a device, the app is evidently unable to access previously downloaded files using the URLs in the database.

    One possible solution, I suppose, is to save the files to a directory in QStandardPaths::writableLocation(QStandardPaths:: DownloadLocation). However, these files will not be cleaned up if the user deletes the app. Moreover, if the user renames or deletes the files for whatever reason, the app will not function properly without additional code that re-fetches any missing files etc. This is a headache, especially with the forthcoming iOS file browser in the Fall.

    I would like to use AppDataLocation as it seems intended for precisely what I want to do. But, how do I get around the apparently incompatible sandbox from build to build?

    Thank you very much for any insight.
    Last edited by Urthas; 9th August 2017 at 00:50.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: iOS AppData directory changes with each build: where to save files?

    I suspect that the problem is saving the absolute URL of a downloaded file, rather than just the filename proper and dynamically generating the URL at runtime...? Will update with a solution (if I find one)!

  3. #3
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: iOS AppData directory changes with each build: where to save files?

    Alright, so, indeed, this problem was not a bug in code per se, but a misunderstanding on my part as to how the iOS app sandbox works. I feel a bit silly, but, lesson learned and maybe this will help someone in future.

    The lesson is: don't persist an absolute device URL that is based on the value returned by QStandardPaths::writableLocation(QStandardPaths:: AppDataLocation). Instead, store the filename (including extension) and stitch together the absolute URL at runtime. For example:

    Qt Code:
    1. // main.cpp
    2. MyApp app;
    3. QQmlApplicationEngine engine;
    4. QQmlContext *rootContext = engine.rootContext();
    5. rootContext->setContextProperty("app", &app);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // myapp.h
    2. Q_PROPERTY(QString dataLocation READ dataLocation CONSTANT)
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // MyView.qml
    2. property var model: ...
    3. property int currentIndex: ...
    4. readonly property url localUrl: "file://" + app.dataLocation + "/" + model.get(currentIndex, <url role>) // <-- gets absolute URL using AppDataLocation and the filename stored in the data layer, at runtime
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 16th August 2011, 00:26
  2. Replies: 2
    Last Post: 25th March 2011, 05:19
  3. QFileDialog 'Save As' to non-existent directory
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2010, 23:32
  4. %APPDATA% equivalent on Symbian
    By bender86 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 4th May 2010, 12:14
  5. how to save sequences of text files and sound files
    By nagpalma in forum Qt Programming
    Replies: 8
    Last Post: 3rd July 2007, 01:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.