3

i am using custom keyboard extension then download keyboard theme from server and save in to app 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.

Now i am trying this but its not working.

let fileManager = FileManager.default
                if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.companyname.appname") {
                    //let newDirectory = directory.appendingPathComponent(dataPath)
                    newDirectory = directory.appendingPathComponent("MyDirectory")
                    try? fileManager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: nil)
                    print(newDirectory.path)
                }
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
ikbal
  • 1,114
  • 1
  • 11
  • 30
  • Did you enable app groups on both targets? – vadian Apr 06 '19 at 10:15
  • yes already do this – ikbal Apr 06 '19 at 10:20
  • 1
    Use the directory which you have created above to save files instead of app document directory. – iphonic Apr 06 '19 at 12:39
  • i am using for get data in extension but its not working like as under – ikbal Apr 06 '19 at 12:47
  • let fileManager = FileManager.default if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.anishmodan.fontapp") { print(directory.path) let pathUrl = directory.appendingPathComponent("Test.jpg") print(pathUrl.path as Any) let Image1 = UIImage(contentsOfFile: pathUrl.path)! //after set this on imageview } – ikbal Apr 06 '19 at 12:47
  • any idea how can i open this file : /private/var/mobile/Containers/Shared/AppGroup/9340E258-6816-4EB4-83E7-34EFDFEA86DB/Test.jpg – ikbal Apr 06 '19 at 12:57

1 Answers1

3

Solved

  let fileManager = FileManager.default
                                        let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.companyname.appname")?.appendingPathComponent("logo.png")

                                        // store image in group container
                                        if !fileManager.fileExists(atPath: url!.path) {

                                            let image =  UIImage(data: data)
                                            let imageData : Data = UIImagePNGRepresentation(image!) as! Data
                                            fileManager.createFile(atPath: url!.path as String, contents: imageData, attributes: nil)
                                        }

//ACCESS THIS or get store image 
 let Image1 = UIImage(contentsOfFile: (url?.path)!)! 
ikbal
  • 1,114
  • 1
  • 11
  • 30
  • I want to write logs in extensions project , i have create the file in app group shared folder but still it could not write data on file , do you know why ? – Anita Nagori Feb 10 '21 at 21:30