CTMacUser
CTMacUser

Reputation: 2042

How do I import, instead of open, files for a NSDocument-based app?

I'm a newbie trying out a web-browser app, using WebKit with the NSDocument architecture (as in some of the WebKit reference code). I have it opening a pre-set home page, then post the current URL to an adjacent text field, then have the WebView open an URL entered into the text field, and then I changed it to load in local files. I did that using the existing NSDocument file-opening code: I override the URL and NSData opening methods to cache both of those (inside the document object), then at XIB-creation time I pass those attributes to the WebView.

It seems to work, but there's been weirdness. I've been trying to work around it, but I think I need a re-think. I finally realized that the local file is still the flagged by the NSDocument architecture even when I change the web page (with the text field) away from the local file. I think I need to "import" the local file instead of "open." One way is to de-certify the file from internal tracking after loading it. The other way is to do a non-tracking read to begin with. Which way is best? And how can I do it in the NSDocument architecture?

Upvotes: 0

Views: 377

Answers (2)

Iikka
Iikka

Reputation: 59

To change the default "Untitled" to something sensible, use NSDocuments setDisplayName method. (Curiously in swift, the displayName is a read-only property but the method .setDisplayName() is available and does the trick (XCode 7.3.1).)

Upvotes: 0

CTMacUser
CTMacUser

Reputation: 2042

Right after posting this, I found out that NSDocument has the fileURL and setFileURL: methods, which control the property of what file the document object is tracking. It can be deliberately set to nil to de-certify the document's current file.

Of course, now exporting uses "Untitled" as the base file for saving the current browser page. The window gets its title from a Cocoa Binding to the WebView's page title property, but that doesn't carry over to the document object. I'll see how to fix that....

Upvotes: 1

Related Questions