Saturday, December 6, 2008

WebKit: Dynamic Content

Today I've played a bit with WebKit. I Want do something like Cocoa VBox View and NSScrollView but with more flexibility. Below you can see the example result.
The interesting part of this example is How to manage WebKit content, and how to add something dynamically.

I use webView:decidePolicyForNavigationAction:request:frame:decisionListener: of WebPolicyDelegate Protocol to handle my custom requests, in this way...

- (void)webView:(WebView *)sender 
         decidePolicyForNavigationAction:(NSDictionary *)actionInformation
         request:(NSURLRequest *)request frame:(WebFrame *)frame
         decisionListener:(id)listener
{
  if ([[[request URL] path] isEqualToString:@"PATH to Handle"]) {
    [listener ignore];
    // Handle Request
  } else {
    [listener use];
  }
}

...Then with a little bit of JavaScript I'll set my dynamic content loaded from Database or somewhere else.

NSArray *args = [NSArray arrayWithObjects:@"Item1Content", 
@"Hi, I'm Test 1 Content", nil];
[[webKit windowScriptObject] callWebScriptMethod:@"fillElement" 
withArguments:args];

The fillElement method is a simple JS function like this document.getElementById(elem).innerHTML = data;

Here you can find the Source Code.

No comments:

Post a Comment