views:

597

answers:

1

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

What i'm trying to do is load a page, have webkit parse it and simply print out the title.

Here is what i've got:

#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;
}

The output is both html and title are blank

Thank You for reading

+2  A: 

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

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.

duncanwilcox
frameLoadDelegate?
jm