user75832
user75832

Reputation:

Accessing DOM Webkit Objective C

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

Upvotes: 2

Views: 2040

Answers (2)

Pierre Houston
Pierre Houston

Reputation: 1641

If you just want to parse the HTML, why not use an NSXMLDocument instead of a WebView. I believe its initWithContentsOfURL initializer blocks until the url is loaded. And if that doesn't work try creating a NSURLRequest for the URL and then use [NSURLConnection sendSynchronousRequest:..] to load it and then feed that to NSXMLDocument.

Upvotes: 0

duncanwilcox
duncanwilcox

Reputation: 3498

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.

Upvotes: 4

Related Questions