Create directory in swift application group container

How can I create a directory in an application group container?

I tried to use as a file manager:

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

but this does not create a directory ... How can I create a directory in this folder?

+4
source share
2 answers

containerURLForSecurityApplicationGroupIdentifierreturns the url to the group container. To create a directory, add a new directory name as a 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)
}
+8
source
  • Check the "Application Group" box . This can be enabled from the project → Features → Application Group → enable .enter image description here

  • "group.com.companyName.exampleApp" , .

  • .

    let appIdentifier = "group.com.companyName.exampleApp"
    let fileManager = NSFileManager.defaultManager()
    let container = fileManager.containerURLForSecurityApplicationGroupIdentifier(appIdentifier)
    
  • , URL- "".

  • ,

    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)
    }
    

: , fooobar.com/questions/110694/...

+1

Source: https://habr.com/ru/post/1629806/


All Articles