views:

205

answers:

1

I'm developing a WebKit Safari Plugin with Xcode. How do I call JavaScript from -webPlugInStart?

+1  A: 

First you should remember the containing view:

+ (NSView *)plugInViewWithArguments:(NSDictionary *)arguments {
    return [[self alloc] initWithArguments:arguments];
}    

- (id)initWithArguments:(NSDictionary*)arguments {
    if((self = [super init])) {
        webView = [[[arguments objectForKey:WebPlugInContainerKey] webFrame] webView];
    }
    return self;
}

When you have that, you can refer to the documentation on "Using JavaScript From Objective-C". E.g.:

- (void)webPlugInStart {
    WebScriptObject *scriptObj = [webView windowScriptObject];
    NSArray *args = [NSArray arrayWithObjects:
        @"someString", [NSNumber numberWithInt:42], nil];
    [scriptObj callWebScriptMethod:@"myJsFunction" withArguments:args];
}
Georg Fritzsche