Custom NSDocument class will not save to file
Custom NSDocument class will not save to file
- Subject: Custom NSDocument class will not save to file
- From: Erik Stainsby <email@hidden>
- Date: Tue, 03 Jan 2012 16:54:01 -0800
I'm certain this is a trivial error on my part, but my custom document class will not write the file to the local filesystem. I have several other files which I can write, and I can write the content when I use an NSString to encode the text first. But once I init my own document, setup the attributedText and try to writeToURL: or saveToURL: phht!
"File could not be saved because the specified format is not supported."
This is the passage in my app delegate which drives the save operation in response to a web page being loaded into a webView:
NSError * error = nil;
self.htmlText = [[NSMutableAttributedString alloc] initWithString: html];
WebHistoryItem * item = [[webview backForwardList] currentItem];
if( ! [history containsObject:item]) {
[history addObject:item];
}
NSString * urlString = [webview mainFrameURL];
NSString * filename = nil;
if( ! [htmlList itemWithTitle:urlString] )
{
[self configureWorkingSiteFolders:urlString];
if(nil==filename)
{
if( [[[NSURL URLWithString:urlString] relativePath] isEqualToString:@"/"])
{
filename = [[@"" stringByAppendingPathComponent:@"index"]
stringByAppendingPathExtension:@"html"];
}
else {
filename = [urlString lastPathComponent];
}
}
// instantiate htmlDocument
RSSHTMLDocument * htmlDocument = [[RSSHTMLDocument alloc] init];
[htmlDocument readFromURL:[NSURL URLWithString:urlString]
ofType:NSHTMLTextDocumentType error:&error];
[htmlDocument setTitle:[webview mainFrameTitle]];
NSURL * fileURL = [[[[NSUserDefaults standardUserDefaults]
URLForKey:kRSSProjectDirectoryURL]
URLByAppendingPathComponent:@"index" isDirectory:NO]
URLByAppendingPathExtension:@"html"];
[htmlDocument saveToURL:fileURL ofType:NSHTMLTextDocumentType
forSaveOperation:NSSaveOperation error:&error ];
if( error ) {
[self presentError:error];
error = nil;
}
[htmlList addItemWithTitle:filename];
}
And here is the custom saveToURL: implementation:
- (BOOL) saveToURL:(NSURL *)url ofType:(NSString *)typeName
forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError {
BOOL saveSuccess = NO;
NSString * page = @"";
NSData * data = [attributedText dataFromRange:NSMakeRange(0, [attributedText length]) documentAttributes:nil error:outError];
if( outError ) {
[NSApp presentError:*outError];
outError = nil;
}
if(!filename) {
filename = [@"unnamed-file" stringByAppendingPathExtension:@"html"];
}
if(data) {
page = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@ [d] %@ %@",[self className],__LINE__, NSStringFromSelector(_cmd),page);
[page writeToURL:[[[NSUserDefaults standardUserDefaults] URLForKey:kRSSCSSDirectoryURL]
URLByAppendingPathComponent:filename isDirectory:NO]
atomically:YES encoding:NSUTF8StringEncoding error:outError];
if( outError ) {
[NSApp presentError:*outError];
outError = nil;
}
saveSuccess = YES;
}
return saveSuccess;
}
My question I suppose is how do I get saveToURL: to write a plaintext version of the contents of my class?
TIA,
Erik
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden