使用 Cocoa 保存 Web 档案
我想使用 Cocoa 以编程方式将网页保存到硬盘作为像 Safari 一样的网络存档。
昨天我搜索了 Apple 的任何示例程序,但我只在 Apple 开发人员页面上找到了 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]);
我希望有人能为我的问题发布解决方案。
预先感谢
迈克尔
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要实例化 WebView 并加载它,但这非常简单:
You'll need to instantiate a WebView and load it, but this it's pretty easy:
我已经实例化了一个 WebView,但我不希望 WebView 此时加载 webarchive。我想下载一个网站作为网络存档并将其保存在硬盘上以供以后使用。稍后我加载此网络存档,然后我将在 Web 视图中显示它。
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.