1

Here is my code, but this code is not working is return me an error for permission.

   let fileManager = FileManager.default

    // Create 'subfolder' directory

    do {
        try fileManager.createDirectory(atPath: "subfolder", withIntermediateDirectories: true, attributes: nil)
    }
    catch let error as NSError {
        print("Ooops! Something went wrong: \(error)")
    }

    let documentsUrl:URL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!

    print("\(documentsUrl)")

** error **

oops! Something went wrong: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “subfolder”."

karel
  • 5,489
  • 46
  • 45
  • 50
Digvijay Gida
  • 79
  • 1
  • 9

1 Answers1

1

I think, you should provide the full path of your new directory as like below.

let applicationDocumentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!

    let fileManager = FileManager.default

    do {
        try fileManager.createDirectory(atPath: applicationDocumentsDirectory + "/subfolder", withIntermediateDirectories: true, attributes: nil)
    }
    catch let error as NSError {
        print("Ooops! Something went wrong: \(error)")
    }
Natarajan
  • 3,241
  • 3
  • 17
  • 34