How to create a simple text file in your iOS App? - Xcode tutorial using swift language

Published: 20 December 2020
on channel: Programmer World
3,201
10

This Video shows simple steps to create a text file from your iOS App. It shows the simple swift code which is written to do create the file in the document directory of the iPhone. It uses Xcode environment to develop and simulate the App in the in-built simulator. It shows the creation of the text file by navigating to the device folder in the library of the Mac OS (host OS) but in the real phone the file should get created in the document folder.


I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

Complete source code and other details can be found in the below link:
https://programmerworld.co/ios/how-to...


However, the main swift code is copied below also for reference:

//
// ViewController.swift
// Text File Creation App
//
// Created by HomePC on 29/10/20.
// Copyright © 2020 HomePC. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func buttonCreate(Sender: UIButton){

let stringToWrite = "This is my demo string by programmer world"

if let dir: NSString = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first! as NSString{

do{
try stringToWrite.write(toFile: "\(dir)/testFile.txt", atomically: true, encoding: String.Encoding.utf8)

print("Successful - file written")
}
catch _{
print("Error")
}

}

}

}


On this page of the site you can watch the video online How to create a simple text file in your iOS App? - Xcode tutorial using swift language with a duration of hours minute second in good quality, which was uploaded by the user Programmer World 20 December 2020, share the link with friends and acquaintances, this video has already been watched 3,201 times on youtube and it was liked by 10 viewers. Enjoy your viewing!