Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

url.isFileReferenceURL() returns false for local file in IOS

Tags:

xcode

ios

swift

I opened a PNG file in my app from another application.

It invokes application(application: UIApplication, handleOpenURL url: NSURL) -> Bool this function.

The following is what I did to test if the coming file is valid:

func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
    var rootViewController : ViewController = self.window?.rootViewController as ViewController
    println("the received url is \(url) and \(url.isFileReferenceURL())")
    if url.isFileReferenceURL() {
        rootViewController.handleOpenURL(url)
        return true
    }

    return false
}

But when debugging, url.isFileReferenceURL() returns false for a example file link like the following:

file:///private/var/mobile/Applications/56-random-identifier/Documents/Inbox/whatever.png

What is happening here? Why is the function return false for a local file? Thank you!

like image 872
donkey Avatar asked Nov 08 '14 12:11

donkey


1 Answers

You have misunderstood what -isFileReferenceURL is about. I'm guessing you're interested in whether the URL is a file URL; that is, whether it refers to a local file rather than a remote resource. For that, you should call -isFileURL or, equivalently, check the fileURL property (e.g. url.fileURL).

File URLs come in two flavors. There are file path URLs and file reference URLs. File path URLs are based on the path, as you would expect. If the referenced file is moved, the URL will not track it to its new location. If a different file is created at the path, the URL will then refer to that new file.

A file reference URL refers to the underlying file object in the file system, independent of the path. If the file is moved within the same volume or renamed, the URL will track the file. It will continue to reference it even though its path has changed.

You can convert back and forth between the two flavors using -filePathURL and -fileReferenceURL. You can determine the flavor using -isFileReferenceURL.

When you create a file URL using any of the WithPath: methods, you get a file path URL. That's why you're seeing -isFileReferenceURL return false.

like image 100
Ken Thomases Avatar answered Oct 04 '22 10:10

Ken Thomases