Charlweed
Charlweed

Reputation: 1614

How to access arbitrary file in OS X 10.10 app?

I am teaching myself Apple Development with a Swift OS X 10.10 App. I want to pass a file URL to NSXMLParser. That file is large, and on a seperate disk from my app. I create the NSURL, but when I check it with checkPromisedItemIsReachableAndReturnError(), I always get a "No such file or directory error".

It looks like whe I run my app in Xcode, something is prepending the app's development directory into my file path, so "/Volumes/bigdrive/data.xml" becomes "/Users/charlweed/Library/Developer/Xcode/DerivedData/dataskunk-ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/file:/Volumes/bigdrive/data.xml"

I did not enable Sandbox, or iCloud when I created the project. I thought I might need to use NSURL.startAccessingSecurityScopedResource() anyway, but it always returns true. What am I doing wrong? Here is a test function in Swift,I don't know objective-c, but I expect I can figure out an objective-c answer:

func accessFile() {
    /**I belive this URI is correct, becuase everything after the file:// works in the OS X bash*/
    let xmlInFilePath = "file:///Volumes/Seagate_1tib/projects/dataskunk/wasteproduct.xml"
    if let xmlInFileURL = NSURL.fileURLWithPath(xmlInFilePath)
    {
        println("Unwrapped \(xmlInFileURL)")
        var securityURLBS = xmlInFileURL.startAccessingSecurityScopedResource()
        if securityURLBS
        {
            var xmlFileError: NSError?
            if xmlInFileURL.checkPromisedItemIsReachableAndReturnError(&xmlFileError)
            {
                println("Can access file. huray!")
                /** Use the file URL**/
            }
            else
            {
                /** This Always happens with a "No such file or directory " :( **/
                println("\(xmlFileError)")
            }
        }
        else
        {
            println("Could not get Security Scoped Resource")
        }
        xmlInFileURL.stopAccessingSecurityScopedResource()
    }
    else
    {
        log(" NSURL.fileURLWithPath() returned nil for \(xmlInFilePath)")
    }
}

Here is the dump of the error:

Unwrapped file:/Volumes/Seagate_1tib/projects/dataskunk/apple_rss.xml -- file:///Users/charlweed/Library/Developer/Xcode/DerivedData/Keepass2Keyring-ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/

Optional(Error Domain=NSCocoaErrorDomain Code=260 "The file “wasteproduct.xml” couldn’t be opened because there is no such file."

UserInfo=0x61000006f9c0 NSURL=file:/Volumes/Seagate_1tib/projects/dataskunk/wasteproduct.xml -- file:///Users/charlweed/Library/Developer/Xcode/DerivedData/Keepass2Keyring-ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/,

NSFilePath=/Users/charlweed/Library/Developer/Xcode/DerivedData/Keepass2Keyring-ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/file:/Volumes/Seagate_1tib/projects/dataskunk/wasteproduct.xml,

NSUnderlyingError=0x610000044a10 "The operation couldn’t be completed. No such file or directory"})

Upvotes: 1

Views: 484

Answers (1)

Charlweed
Charlweed

Reputation: 1614

The answer is that NSURL.fileURLWithPath() does not take a URL-path as an argument, only a filesystem-path. So "file:///Volumes/disk/file.xml" is wrong, "/Volumes/disk/file.xml" is correct.

The mangling is NSURL prefixing the current directory onto what it thinks is a relative filesystem-path String.

Upvotes: 1

Related Questions