views:

271

answers:

2

Hello,

I would like to save a webpage programmatically with Cocoa to the hard disk as a webarchive like Safari.

I've searched for any example programs from Apple yesterday, but I've only found a class reference at Apple Developers page for a webarchive class. I play a little bit with the webarchive class and unfortunately my solution don't give any ouput.

NSURL *url = [NSURL URLWithString:@"http://www.google.de"];
NSURLRequest *urlRequest = (NSMutableURLRequest*)[NSURLRequest requestWithURL:url
                                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                              timeoutInterval:30.0];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                returningResponse:&response
                                            error:&error];
WebArchive *wa = [[WebArchive alloc] initWithData:urlData];
NSData *waData = [wa data];
NSLog(@"%@", [[NSString alloc] initWithData:waData encoding:NSUTF8StringEncoding]);

I hope somebody could post a solution for my problem.

Thanks in advance

Michael

+3  A: 

You'll need to instantiate a WebView and load it, but this it's pretty easy:

[[[webView mainFrame] dataSource] webArchive]
Ben Gottlieb
A: 

I've instantiate a WebView but I don't want that the WebView loads the webarchive at this time. I would like to download a website as a webarchive and save it for later on the hard disk. Later than I load this webarchive and then I will show it in a WebView.

Michael
The webview will download the page, and then you can save it. I'm not sure what you don't want to happen.
Ben Gottlieb
Yes, you don't need to put the WebView onscreen in a window, you can just use it programmatically to create the web archive and then dispose of it.
Rob Keniger