从 iOS 中的 ApplicationSupportDirectory 读取
Reading from ApplicationSupportDirectory in iOS
使用 LXLogger,我能够将日志条目写入设备和模拟器上的文件(除了 xcode 控制台等)。
LXLogger 存储文件的默认位置在 ApplicationSupportDirectory 下。
LXLogger 写入文件正常,我可以看到文件在我的设备上(或者在使用模拟器时在我的 mac HDD 上),但是当我尝试打开文件时,我被告知该文件不存在。
我添加了根据代码生成的错误日志。
我在这里尝试了十几种不同的方法和多个问题,但无济于事。
let directory = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask).first!.URLByAppendingPathComponent(NSBundle.mainBundle().bundleIdentifier!, isDirectory: true).URLByAppendingPathComponent("logs", isDirectory: true)
do {
let array = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(directory, includingPropertiesForKeys: nil, options:[])
var str = array[0].description
str = str.stringByReplacingOccurrencesOfString("file://", withString: "", options: .LiteralSearch, range: nil)
let fileURL = NSURL(fileURLWithPath:str)
log.info("fileURL contains: " + fileURL.absoluteString)
do {
log.info("Attempting to get contentsOfURL")
let data = try NSString(contentsOfFile: array[0].description,
encoding: NSASCIIStringEncoding)
logTextView.text = data as String
} catch let error as NSError {
log.error(error.description)
}
}
catch let error as NSError {
log.error(error.description)
}
记录上述代码的输出:
> 2016-03-31 21:43:52.121 [INFO] fileURL contains: file:///private/var/mobile/Containers/Data/Application/2F812C06-E204-4543-905C-F884C35DA198/Library/Application%2520Support/myname.myapp/logs/1_log.txt
> 2016-03-31 21:43:52.122 [INFO] Attempting to get contentsOfURL
> 2016-03-31 21:43:52.123 [ERROR] Error Domain=NSCocoaErrorDomain Code=260 "The file “1_log.txt” couldn’t be opened because there is no such file." UserInfo={NSFilePath=file:///private/var/mobile/Containers/Data/Application/2F812C06-E204-4543-905C-F884C35DA198/Library/Application%20Support/myname.myapp/logs/1_log.txt, NSUnderlyingError=0x1376e0e00 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
所以代码显示文件在那里(我可以在我的 mac 上物理打开副本),但应用程序找不到它。
大概和我这样使用文件handle/nsurl/something有关??
您从 array
获取 NSURL 并使用 stringByReplacingOccurrencesOfString
和 fileURLWithPath
然后 description
的方法是错误的;它没有正确处理 URL 中的百分比编码,通常是 complex/fragile.
这可以简单得多,因为您已经从 contentsOfDirectoryAtURL
的数组中返回了一个文件 URL。你可以直接使用它:
let data = try String(contentsOfURL: array[0], encoding: NSUTF8StringEncoding)
使用 LXLogger,我能够将日志条目写入设备和模拟器上的文件(除了 xcode 控制台等)。 LXLogger 存储文件的默认位置在 ApplicationSupportDirectory 下。
LXLogger 写入文件正常,我可以看到文件在我的设备上(或者在使用模拟器时在我的 mac HDD 上),但是当我尝试打开文件时,我被告知该文件不存在。
我添加了根据代码生成的错误日志。
我在这里尝试了十几种不同的方法和多个问题,但无济于事。
let directory = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask).first!.URLByAppendingPathComponent(NSBundle.mainBundle().bundleIdentifier!, isDirectory: true).URLByAppendingPathComponent("logs", isDirectory: true)
do {
let array = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(directory, includingPropertiesForKeys: nil, options:[])
var str = array[0].description
str = str.stringByReplacingOccurrencesOfString("file://", withString: "", options: .LiteralSearch, range: nil)
let fileURL = NSURL(fileURLWithPath:str)
log.info("fileURL contains: " + fileURL.absoluteString)
do {
log.info("Attempting to get contentsOfURL")
let data = try NSString(contentsOfFile: array[0].description,
encoding: NSASCIIStringEncoding)
logTextView.text = data as String
} catch let error as NSError {
log.error(error.description)
}
}
catch let error as NSError {
log.error(error.description)
}
记录上述代码的输出:
> 2016-03-31 21:43:52.121 [INFO] fileURL contains: file:///private/var/mobile/Containers/Data/Application/2F812C06-E204-4543-905C-F884C35DA198/Library/Application%2520Support/myname.myapp/logs/1_log.txt
> 2016-03-31 21:43:52.122 [INFO] Attempting to get contentsOfURL
> 2016-03-31 21:43:52.123 [ERROR] Error Domain=NSCocoaErrorDomain Code=260 "The file “1_log.txt” couldn’t be opened because there is no such file." UserInfo={NSFilePath=file:///private/var/mobile/Containers/Data/Application/2F812C06-E204-4543-905C-F884C35DA198/Library/Application%20Support/myname.myapp/logs/1_log.txt, NSUnderlyingError=0x1376e0e00 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
所以代码显示文件在那里(我可以在我的 mac 上物理打开副本),但应用程序找不到它。 大概和我这样使用文件handle/nsurl/something有关??
您从 array
获取 NSURL 并使用 stringByReplacingOccurrencesOfString
和 fileURLWithPath
然后 description
的方法是错误的;它没有正确处理 URL 中的百分比编码,通常是 complex/fragile.
这可以简单得多,因为您已经从 contentsOfDirectoryAtURL
的数组中返回了一个文件 URL。你可以直接使用它:
let data = try String(contentsOfURL: array[0], encoding: NSUTF8StringEncoding)