mad_dog
mad_dog

Reputation: 158

Reading from ApplicationSupportDirectory in iOS

Using LXLogger I am able to write log entries to file on the device and simulator (in addition to xcode console etc). The default location which LXLogger stores the file is under the ApplicationSupportDirectory.

LXLogger writes to the file OK, and I can see the file is on my device (or on my mac HDD when using the simulator), but when I try to open the file I am told that the file does not exist.

I have added the error logs generated in line with the code.

I've tried a dozen different methods and multiple questions on here, to no avail.

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

Log Outputs from above code:

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

So the code shows that the file is there (and i can physically open the copy on my mac), but the app can't find it. Presumably its something to do with my usage of the file handle/nsurl/something like that??

Upvotes: 0

Views: 338

Answers (1)

jtbandes
jtbandes

Reputation: 118651

Your approach of taking the NSURL from array and using stringByReplacingOccurrencesOfString and fileURLWithPath and then description is wrong; it doesn't correctly handle the percent-encoding in the URL and it's generally complex/fragile.

This can be much simpler, because you already have a file URL returned in the array from contentsOfDirectoryAtURL. You can just use it directly:

let data = try String(contentsOfURL: array[0], encoding: NSUTF8StringEncoding)

Upvotes: 2

Related Questions