我想用Cocoa以编程方式将网页保存为硬盘,例如Safari。

昨天我从Apple搜索了任何示例程序,但是我只在Apple Developers页面上找到了一个webarchive类的类引用。我在webarchive课上玩了一点,不幸的是我的解决方案没有给出任何输出。

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]);


我希望有人可以为我的问题提供解决方案。

提前致谢

麦可

最佳答案

您需要实例化一个WebView并加载它,但这很简单:

[[[webView mainFrame] dataSource] webArchive]

07-25 22:53