Create Share app Group Directory Folder and Access All image from folder

Sohan
1 min readApr 22, 2021
Photo by Annika Gordon on Unsplash

Create Folder in Share App Group Directory

let image = UIImage.(named : "imageName")

let fileManager = FileManager.default

let path = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.companyName.appname")?.appendingPathComponent("FolderName")

if !fileManager.fileExists(atPath: path!.path) {

try! fileManager.createDirectory(atPath: path!.path, withIntermediateDirectories: true, attributes: nil)

}

let url = NSURL(string: path!.path)

let imagePath = url!.appendingPathComponent(imageName)

let urlString: String = imagePath!.absoluteString

let imageData = image.jpegData(compressionQuality: 1.0)

fileManager.createFile(atPath: urlString as String, contents: imageData, attributes: nil)

Access All Image From Share App group Folder

class GetAllImages: NSObject {

static func loadImagesFromAlbum(folderName:String) -> (String,[String]){

let fileManager = FileManager.default

let path = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.compnyname.appname")

var theItems = [String]()

var StaticUrl = String()

if let dirPath = path?.path

{

let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(folderName)

StaticUrl = String(describing: imageURL)

do {

theItems = try FileManager.default.contentsOfDirectory(atPath: imageURL.path)

return (StaticUrl,theItems)

} catch let error as NSError {

print(error.localizedDescription)

return (StaticUrl,theItems)

}
}

return (StaticUrl,theItems)

}
}

Above function are very helpful when you are using extensions and you need shared data from app.

--

--