简体   繁体   English

在应用扩展程序的共享容器中找不到文件

[英]Cannot find file in app extension's shared container

I want to write log file at my extension, and read it at my app. 我想在扩展名中写入日志文件,并在我的应用程序中读取它。 For this purpose, I'm using shared groups (so both the app and the extension would be able to read from the same file) 为此,我使用共享组(因此应用程序和扩展名都可以从同一文件读取)

I wrote the following code: 我写了以下代码:

Extension: 延期:

let fileManager = NSFileManager.defaultManager()
let containerUrl = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.MyCompany.MyProj")

let extensionLogDirectory = containerUrl?.path?.stringByAppendingString("AppExtensionLogs")
let logFileManager = DDLogFileManagerDefault(logsDirectory: extensionLogDirectory)
PacketTunnelProvider.fileLogger = DDFileLogger(logFileManager: logFileManager)

PacketTunnelProvider.fileLogger!.rollingFrequency = 60*60*12
PacketTunnelProvider.fileLogger!.logFileManager.maximumNumberOfLogFiles = 1
DDLog.addLogger(PacketTunnelProvider.fileLogger)

App (just to read the log file): App(仅读取日志文件):

let fileManager = NSFileManager.defaultManager()
        let containerUrl = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.MyCompany.MyProj")
        if let extensionLogDirectory = containerUrl?.path?.stringByAppendingString("AppExtensionLogs") {
            do {
                let directoryContents = try fileManager.contentsOfDirectoryAtPath(extensionLogDirectory)//always fails
                for file in directoryContents {
                    let path = extensionLogDirectory.stringByAppendingString(file)
                    do {
                        let fileContents = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding)
                        NSLog("file: \(fileContents)")
                    }
                    catch {/* error handling here */

                    }
                }
            }
            catch {/* error handling here */
                 NSLog("nope!")
            }

But, something now right - it's seems like contentsOfDirectoryAtPath always fails with "no such file" error What's wrong in this code? 但是,现在正确了-似乎contentsOfDirectoryAtPath总是失败,并显示“无此类文件”错误该代码有什么问题?

The problem is unrelated to app extensions or CocoaLumberjack. 该问题与应用程序扩展或CocoaLumberjack无关。

stringByAppendingString just concatenates strings, so that the path separator "/" is missing in the generated directory name. stringByAppendingString仅连接字符串,因此在生成的目录名称中缺少路径分隔符“ /”。

There was a dedicated method stringByAppendingPathComponent , which however has been deprecated in Objective-C and is no longer available in Swift. 有一个专用的方法stringByAppendingPathComponent ,但是在Objective-C中已弃用,在Swift中不再可用。 You should operate on the URL by using URLByAppendingPathComponent instead: 您应该改为使用URLByAppendingPathComponent对URL进行URLByAppendingPathComponent

let extensionLogDirectory = containerUrl?.URLByAppendingPathComponent("AppExtensionLogs").path

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建/使用共享应用程序组容器作为包含应用程序及其在ios中的扩展名之间的缓存存储 - How to create/use a shared app group container as cache storage between containing app and it's extension in ios 应用扩展程序和主应用之间的共享文件 - Shared file between app extension and main app 与应用程序的共享扩展名共享时,不会调用UIDocumentInteractionControllerDelegate方法 - UIDocumentInteractionControllerDelegate methods not getting called when shared to an app's share extension 无法从iOS 10中的App组共享容器中获取扩展程序中的图像 - Not able to fetch images in extension app from App group shared container in iOS 10 是否有比NSUserDefaults更安全的容器来持久保存应用程序和扩展之间的共享数据? - Is there a more secure container than NSUserDefaults for persisting shared data between an app and an extension? 如何保证主机应用程序和扩展程序使用的共享应用程序容器中的Core Data存储中的唯一条目? - How can I guarantee unique entries in a Core Data store in a shared app container used by both the host app and an extension? 无法在此文件中预览 - 无法获取 [AppName].app 的沙盒容器位置 - Cannot preview in this file - Failed to get the location of the sandbox container for [AppName].app 是否可以从扩展名的主应用程序访问文件? - is it able to access the file from main app in it's extension? iOS8 无法在应用组共享容器位置下创建文件夹/文件 - iOS8 Unable to create a folder/file under app group shared container location 应用程序和扩展程序之间共享的单例类 - singleton class shared between app and extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM