0

I am trying to create a WebArchive from a WebView. I have it working, but I have found a problem when I try and work with edited content. This is what I am currently doing:

[webView setEditable:YES];
WebDataSource *dataSource = [[webView mainFrame] dataSource];

WebArchive *archive = [[WebArchive alloc]
        initWithMainResource:[dataSource mainResource]
        subresources:nil
        subframeArchives:nil];

[[archive data] writeToFile:destinationPath atomically:YES];
[[webView mainFrame]
 loadRequest:[NSURLRequest
     requestWithURL:
     [NSURL fileURLWithPath:destinationPath]
     ]];

Any edits I make to the content in the WebView do not get stored in the WebArchive. Do I need to commit my changes back to the original file in order for this to work? I would like it to save based on the content that is in the WebView. Any help would be appreciated. Thanks!

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
David
  • 1,674
  • 1
  • 21
  • 35
  • That's what I'm doing and works OK here (i.e. writeToFile:atomically:). I guess there is something wrong with loading webarchive's content back to webview. Can you check if the file you've written in destinationPath is what you want, i.e. the edited one? – Piotr Byzia Jan 14 '11 at 07:25
  • I am actually loading an HTML file (input.html) into the WebView and then trying to save that into a WebArchive (output.webarchive). The saving works, but it seems to create the WebArchive from the original file and ignores the changes made. When I say changes I mean that after setting [webView setEditable:YES] and then I delete or add text, then I save the WebArchive, none of my changes make it into the output. Is that what you say you have working? – David Jan 16 '11 at 00:26
  • [dataSource mainResource] returns "A web resource representing the data source. The contents returned are based on the original downloaded data. You can use the returned value to create a WebArchive object instead of using the webArchive method." according to Apple Reference Library (hhttp://developer.apple.com/library/mac/documentation/cocoa/Reference/WebKit/Classes/WebDataSource_Class/Reference/Reference.html). At this point I believe I need a way to grab the source DOM from the WebView, then create a WebDataSource, then a WebArchive. Any thoughts on how to do that? – David Jan 20 '11 at 23:07

1 Answers1

0

After digging for awhile, I was able to figure out the answer to my own question. Here is how I did it:

WebResource *dataSource = [[[[webView mainFrame] DOMDocument] webArchive] mainResource];

WebArchive *archive = [[WebArchive alloc]
       initWithMainResource:dataSource
       subresources:nil
       subframeArchives:nil];

[[archive data] writeToFile:@"output.webarchive" atomically:YES];

Hope that helps someone.

David
  • 1,674
  • 1
  • 21
  • 35