class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let str = "Test Message" let url = self.getDocumentsDirectory().appendingPathComponent("message.txt") do { try str.write(to: url, atomically: true, encoding: .utf8) } catch { print(error.localizedDescription) } } 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, which ought to be the only one return paths[0] } }