问题描述
可能有人谁是熟悉的webkit请解释或点我在正确的方向,为什么下面的code不起作用
Could someone who is familiar with webkit please explain or point me in the right direction as to why the following code does not work
我想要做的是加载一个页面,都WebKit的分析它,并简单地打印出标题。
What i'm trying to do is load a page, have webkit parse it and simply print out the title.
下面是我已经得到了:
#include <iostream>
#include <WebKit/WebKit.h>
using namespace std;
/*
Seek Help
*/
int main (int argc, char * const argv[]) {
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
WebFrame * mainFrame;
WebView * view = [[WebView alloc] initWithFrame: NSMakeRect (0,0,640,480)];
mainFrame = [view mainFrame];
NSString * url = @"http://test.com";
[[view mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
NSString * data = @"<html><head><title>testing</title></head><body>tester</body></html>";
[[view mainFrame] loadHTMLString:data baseURL: nil];
// NSString * urlString = [[NSString alloc] initWithUTF8String:"<html><head><title>Hello World</title></head><body><p>My first Web page.</p></body></html>"];
//[[view mainFrame] loadHTMLString:urlString baseURL:nil];
NSString * outerHtml = [(DOMHTMLElement *)[[[view mainFrame] DOMDocument] documentElement] innerHTML];
cout << "Html: " << [outerHtml UTF8String] << endl;
NSString * title = [view mainFrameTitle];
cout << "title: " << [title UTF8String] << endl;
[autoreleasepool release];
return 0;
}
输出是HTML和标题都是空白的。
The output is both html and title are blank
感谢您阅读
推荐答案
它不起作用,因为WebKit的依赖RunLoop加载的内容,即使它只是静态内容。
It doesn't work because WebKit relies on the RunLoop to load content, even if it's just static content.
您应该添加一个标准运行的循环,如:
You should add a standard run loop such as:
while (!done) {
pool = [[NSAutoreleasePool alloc] init];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantPast]];
[pool release];
}
和创建你设置为WebFrameLoadDelegate( webView.frameLoadDelegate = thatObject
)的自定义类,并在 - (无效)web视图:(的WebView *)发件人didFinishLoadForFrame:(WebFrame *)帧
回调,你应该能够访问DOM。此外,当负载在完成
标志位来完成设置为YES。
And create a custom class that you set as WebFrameLoadDelegate (webView.frameLoadDelegate = thatObject
), and in the - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
callback you should be able to access the DOM. Also when the load is done set the done
flag to YES.
这篇关于访问DOM的Webkit目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!