【问题标题】:how can I create and access share app group document directory?如何创建和访问共享应用程序组文档目录?
【发布时间】:2019-08-28 02:20:51
【问题描述】:

我正在使用自定义键盘扩展,然后从服务器下载键盘主题并保存到应用程序文档目录,但应用程序文档目录无法在自定义键盘扩展程序中访问,因此需要共享应用程序组目录。我该怎么办,请帮帮我。

现在我正在尝试这个,但它不起作用。

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

【问题讨论】:

  • 您是否在两个目标上都启用了应用组?
  • 是的,已经这样做了
  • 使用上面创建的目录而不是app文档目录来保存文件。
  • 我用于在扩展中获取数据,但它不像下面那样工作
  • 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)! //在imageview上设置后}

标签: swift custom-keyboard ios-app-group document-directory


【解决方案1】:

解决了

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

【讨论】:

  • 我想在扩展项目中写日志,我在应用组共享文件夹中创建了文件,但它仍然无法在文件中写入数据,你知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多