Last active
March 30, 2021 00:10
-
-
Save PraveenKommuri/8757a1adece686ad1c0089b036b89cc8 to your computer and use it in GitHub Desktop.
Get the document directory. save the video file & txt file & read the same.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Get the app document direcitory path | |
/// - Returns: path as URL | |
func getDocumentsDirectory() -> URL { | |
// Find all possible documents directories for this user | |
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) | |
// just send back the first one, Usually it will be only one | |
return paths[0] | |
} | |
// How to use | |
//save the video to app directory | |
let videoData = try? Data(contentsOf: selectedVideo) | |
let documentsDirectory: URL = getDocumentsDirectory() | |
let dataPath = documentsDirectory.appendingPathComponent("video.mp4") | |
try? videoData?.write(to: dataPath) | |
// Save the txt file into app directory. | |
let sampleString: String = "test string" | |
let pathUrl = documentsDirectory.appendingPathComponent("file1.txt") | |
try? sampleString.write(to: pathUrl, atomically: true, encoding: .utf8) | |
// Reading the saved txt file. | |
let input = try? String(contentsOf: pathUrl) | |
print("input is: \(input ?? "")") | |
//Check the file existings or not | |
let documentsDirectory: URL = getDocumentsDirectory() | |
let filePath = documentsDirectory.appendingPathComponent("/video.mp4") | |
let fm = FileManager() | |
if fm.fileExists(atPath: filePath.relativePath) { | |
// File exists. Do the other operations. | |
} else { | |
// File does not exist. Handle negative cases. | |
} | |
//Print the list of files in document directory | |
let fileUrls = try? fm.contentsOfDirectory(atPath: getDocumentsDirectory()) | |
print("fileurls: \(String(describing: fileUrls))") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment