Where can a sandboxed Mac app save files?

前端 未结 4 1591
你的背包
你的背包 2021-02-03 23:45

My Mac app is sandboxed and I need to save a file. Where do I save this file? I can\'t seem to find the specific place where this is allowed without using an open panel. This is

4条回答
  •  半阙折子戏
    2021-02-04 00:18

    Converting @Mazyod's answer into Swift (5.1):

    var appPath: URL? {
        //Create App directory if not exists:
        let fileManager = FileManager()
        let urlPaths = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)
        if let bundleID = Bundle.main.bundleIdentifier, let appDirectory = urlPaths.first?.appendingPathComponent(bundleID,isDirectory: true) {
            var objCTrue: ObjCBool = true
            let path = appDirectory.path
            if !fileManager.fileExists(atPath: path,isDirectory: &objCTrue) {
                do {
                    try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
                } catch {
                    return nil
                }
            }
            return appDirectory
        }
        return nil
    }
    

    However, the directory has changed and I am not sure that the additonal repetition of the bundle ID is needed as the path is

    "/Users/**user name**/Library/Containers/**bundleID**/Data/Library/Application Support/**bundleID**". 
    

    But it seems to work.

提交回复
热议问题