Search code examples
cocoaresourceswebviewwebarchive

How do you save to WebArchive webView editable content?


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!


Solution

  • 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.