0

I Download pdf file Like This And Saved the Path in Userdefault var documents = PDFDocument

let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let fileName = urlString as NSString;
var arrayUrl = [String]()
arrayUrl.append(filePath)
self.defaults.set(arrayUrl, forKey: Constants.myKeyURL)

The First Time when App Run This documents get Value Well But Next Time When I don't need to Download Again documents it's Null but The ArrayURL ints same Value !!

var arrayUrl = [String]()

self.defaults.stringArray(forKey: Constants.myKeyURL)
arrayUrl = self.defaults.stringArray(forKey: Constants.myKeyURL)!

self.documents = arrayUrl.flatMap { PDFDocument(url: URL(fileURLWithPath: $0) ) }

print(self.documents)

DispatchQueue.main.async {

self.tableView.reloadData()

}
user2296278
  • 528
  • 1
  • 4
  • 17
  • You should also read the comments posted in your previous question and the edit was made to the accepted answer which already warned you about your current issue – Leo Dabus Feb 01 '18 at 03:19
  • Note that would be much easier to simply use FileManager cotentsOfDirectory method to list your pdf documents located at your document directory when needed. No need to save anything to user defaults. https://stackoverflow.com/questions/27721418/getting-list-of-files-in-documents-folder/27722526?s=1|64.9724#27722526 – Leo Dabus Feb 01 '18 at 03:23
  • @LeoDabus I Need to use the UserDefault Because after I need to Reorder or Something like this can you help me more – user2296278 Feb 01 '18 at 09:57

1 Answers1

4

You shouldn't save the file's fullpath, for security reasons your app's container name gets changed, so the document directory would also changed every time your app gets launched/reloaded.

Instead, save only the file's name along its extension (use the property lastPathComponent from the downloaded url), and whenever you want to load these files just append the name to the document directory.

Lamour
  • 3,002
  • 2
  • 16
  • 28
  • how to Save lastPathComponent and take back this for read Please help me let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let fileName = urlString as NSString; let filePath="\(documentsPath)/\(fileName.lastPathComponent)"; let fileExists = FileManager().fileExists(atPath: filePath) – user2296278 Feb 01 '18 at 09:56