9

How can I go about creating a directory in my App's group container?

I've tried using as the file manager:

let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("APP_GROUP_IDENTIFIER")!

but that doesn't create a directory...How can I go about creating a directory in this folder?

user3076658
  • 93
  • 1
  • 1
  • 4

2 Answers2

13

containerURLForSecurityApplicationGroupIdentifier returns the URL to the group container.
To create a directory append the new directory name as path component

let fileManager = NSFileManager.defaultManager()
if let directory = fileManager.containerURLForSecurityApplicationGroupIdentifier("APP_GROUP_IDENTIFIER") {
    let newDirectory = directory.URLByAppendingPathComponent("MyDirectory")
    try? fileManager.createDirectoryAtURL(newDirectory, withIntermediateDirectories: false, attributes: nil)
}

Swift 3:

let fileManager = FileManager.default
if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_IDENTIFIER") {
    let newDirectory = directory.appendingPathComponent("MyDirectory")
    try? fileManager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: nil)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • 1
    This one should be now: `let fileManager = FileManager.default if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_IDENTIFIER") { let newDirectory = directory.appendingPathComponent("my_dir") try! fileManager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: nil) }` A question: before creating a directory, isn't required to test if it exists a directory with the same name? –  Nov 26 '16 at 09:21
  • 1
    @IanBell It's good programming habit to test if an directory exists. You could set `withIntermediateDirectories` to true or use `try?` or `try` with a `do - catch` block. – vadian Nov 26 '16 at 09:49
  • hi @vadian i want to create folder in shared document directory and how to access folder data from share document directory. – ikbal Apr 06 '19 at 09:25
  • What does *shared document directory* mean? – vadian Apr 06 '19 at 09:28
  • i am using custom keyboard extension then download keyboard theme from server and save in to document directory but app document directory can not access in custom keyboard extension so, required to share app group directory. how can i do please help me. – ikbal Apr 06 '19 at 09:50
  • @ikbal Please ask a new question – vadian Apr 06 '19 at 09:51
  • https://stackoverflow.com/questions/55547930/how-can-i-crate-and-access-share-app-group-document-directory – ikbal Apr 06 '19 at 09:59
9
  1. Check you enable "App Group" entitlement. It can be enable from project -> Capabilities -> App Group -> switch on.enter image description here

  2. Add an app group identifier "group.com.companyName.exampleApp" as above image.

  3. Now you can access app group container using the specified identifier.

    let appIdentifier = "group.com.companyName.exampleApp"
    let fileManager = NSFileManager.defaultManager()
    let container = fileManager.containerURLForSecurityApplicationGroupIdentifier(appIdentifier)
    
  4. If you set everything properly, you will get an URL address in "container".

  5. Now,

    do{
       if let container = container {
    
    
        let directoryPath  = container.URLByAppendingPathComponent("sampleDirectory")
    
        var isDir : ObjCBool = false
        if let path = directoryPath?.path where fileManager.fileExistsAtPath(path, isDirectory:&isDir) {
          if isDir {
            // file exists and is a directory
          } else {
            // file exists and is not a directory
          }
        } else if let directoryPath = directoryPath {
          // file or directory does not exist
          try fileManager.createDirectoryAtURL(directoryPath, withIntermediateDirectories: false, attributes: nil)
        }
      }
    } catch let error as NSError {
      print(error.description)
    }
    

courtesy: file & directory checking code taken from https://stackoverflow.com/a/24696209/2666902

Community
  • 1
  • 1
Milan Kamilya
  • 2,188
  • 1
  • 31
  • 44