Remove the file from document directory iOS swift

 Solution:

// MARK: - remove the document directory file

func removeFileDoc(itemName: String) {

    let fileManager = FileManager.default

    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory

    let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask

    let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)

    guard let dirPath = paths.first else {

        return

    }

    if itemName != "" {

        let filePath = "\(dirPath)/\(itemName)"

        do {

            try fileManager.removeItem(atPath: filePath)

        } catch let error as NSError {

            print(val:error.debugDescription)

        }

    }

}


You can send the filename to the above function.It will delete the file that was stored in document directory will be deleted.

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift