简体   繁体   中英

How to get files which are locally stored in app's document directory after ios app version get update?

I have developed an iOS app that has been downloading files and storing data in the Documents directory. I am storing document's directory path (where downloaded files get store) in sqlite database. Now if App version get changes then path for database file and document's directory get changes. If I have manually delete the app and install it again then downloaded files get removed from document's directory and user has to download files again. Is there any way to get my database file with previously downloaded document's directory path so that no need to download files again Or any other solution for this?

Understand this concept for document directory

If you update the newer version of the App, Data in the document directory will not getting erased. It will be there. Just the path to reach the document directory get's updated.

So, to overcome the path changing problem. Don't store the entire path to your database file. Just store the ending path or name of the downloaded content to the document directory. Now, if you wants to get some file, lets say example.jpg from the document directory, then you should first get the path to document directory and then append the path of your file.

PATH_TO_DOCUMENT_DIRECTORY/example.jpg this is the complete path for your image. and same is applies for your database.sqlite file.

You can use this handy functions:

func documentsDirPath() -> String {
    var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as [String]
    return paths[0]
}

And can call it in this way:

let pathToDocumentDirectory = documentsDirPath()
    let fileName = "example.jpg" // This you can get from the database.
    let filePath = pathToDocumentDirectory + "/\(fileName)"
    print(pathToDocumentDirectory)

And for the app that get's deleted, there is no option to recover the contents of the document directory, unless you code and save the data to iCloud and on newer installation of the app, in applicationDidFinishLaunching check if there is some files, present on iCloud with name database.sqlite and other, then you can fetch it and put it to document directory and later you can use.

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM