Save File to directory and Fetch in iOS
In iPhone, you can’t save any file in iPhone without an image. if you want to save any file with extensions mp3, pdf, doc, apppleHTML etc in iPhone you can’t save to iPhone by apple not give permissions to do that. So to do that first you have to save all these files to the app directory. here is the example that you have any text and you can save it in the format of pdf in the app directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
let file = "file.pdf" //this is the file. we will write to and read from it let text = self.StringData //just a text if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { let path = dir.appendingPathComponent(file) //writing do { try text.write(to: path, atomically: false, encoding: String.Encoding.utf8) } catch {/* error handling here */} //reading do { let text2 = try String(contentsOf: path, encoding: String.Encoding.utf8) print(path) print(text2) // let fileManager = FileManager.default print(path) // path of file or you can say url } catch {/* error handling here */} } }) |