Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func createDocument(folderName: String ,completion: @escaping(Bool,String)->()){
- let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
- if let documentsDirectory = path{
- //
- let docDirectoryPath = documentsDirectory.appending(folderName)
- let fileManager = FileManager.default
- if !fileManager.fileExists(atPath: docDirectoryPath) {
- do {
- try fileManager.createDirectory(atPath: docDirectoryPath,
- withIntermediateDirectories: true,
- attributes: nil)
- //return path on success
- completion(true,docDirectoryPath)
- return
- } catch {
- print("Error creating folder in documents dir: (error.localizedDescription)")
- completion(false,error.localizedDescription)
- return
- }
- }
- completion(true,docDirectoryPath)
- }
- }
- func downloadDocument(){
- createDocument(folderName: "/chat/doc") { (status, path) in
- if let docURL = self.documentURL{
- if status{
- //folder is created now download document
- let docDownloadRef = Storage.storage().reference(forURL: docURL)
- //get documents metadata from firebase to get document name
- docDownloadRef.getMetadata { metadata, error in
- if let error = error {
- // Uh-oh, an error occurred!
- } else {
- //get document name from metadata
- if let fileName = metadata?.name{
- //create file system url to store document
- let docsurl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
- let myurl = docsurl.appendingPathComponent("chat/(fileName)")
- _ = docDownloadRef.write(toFile: myurl) { url, error in
- if let error = error {
- print(error.localizedDescription)
- } else {
- // Local file URL for document is returned
- print("doc url (url?.absoluteString)")
- }
- }
- }
- }
- }
- }else{
- //folder is not created show document using online location
- }
- }
- }
- }
- Ultimate goal is to achieve something like this "Documents/chat/doc/abc.doc"
Add Comment
Please, Sign In to add comment