NSDocument read RTFD Data
-
20-09-2019 - |
Question
I am working on a text editor and I want to add RTFD support. After adding the RTFD file type to my Info.plist, I got the message that "readFromFileWrapper:ofType:error: must be overridden for your application to handle file packages."
I had a look around in the documentation and found some things, but I didn't manage to do everything..Could someone be so kind and help me out please?
- (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError **)outError {
if ([typeName isEqualToString:@"Rich Text with Attachments"]) {
NSLog(@"Its RTFD");
//it gets to here but I don't know how to load the rtfd data then :(
}
ofType:typeName] return YES; }
Any help is apprechiated :)
Solution
RTFD is a particularly easy one, luckily. Check out the readRTFDFromFile:
and writeRTFDToFile:atomically:
methods of NSText
.
From the NSText Class Reference:
readRTFDFromFile:
Attempts to read the RTFD file at path, returning
YES
if successful andNO
if not.
- (BOOL)readRTFDFromFile:(NSString *)path
Discussion
path should be the path for an.rtf
file or an.rtfd
file wrapper, not for the RTF file within an.rtfd
file wrapper.
If you're coming from NSURL
, do something like this:
[textView readRTFDFromFile:[[self fileURL] path]];
To be safe, call [[self url] isFileURL]
first.
OTHER TIPS
I tried your snippet and achieved the following:
The NSDocument window was opened, the title was updated to the RTFD file's name but the NSTextView is still blank.
I was pretty convinced that my code would open it, since I got myself the URL from the NSDocument instance, converted it to an NSString and sent it to the textview...
if ([typeName isEqualToString:@"Rich Text with Attachments"]) {
NSString *urlString = [[self fileURL] absoluteString];
NSLog(@"%@",urlString);
[textView readRTFDFromFile:urlString];
return YES;
}
The Log returned this:
file://localhost/Users/David/Documents/Coursework/Geography%20Coursework.rtfd/
So it should work?