當你的插件視圖被實例化,WebKit的調用您的視圖的主類的+plugInViewWithArguments:
方法。
該方法的參數參數是一個字典,您可以查詢各種信息。在你的情況下,你需要對應於WebPlugInContainerKey
的對象。
這是符合WebPlugInContainer
非正式協議的對象。如果它不是nil
,則可以要求該對象獲取其-webFrame
,該對象將返回一個WebFrame
對象。然後您可以詢問WebFrame
對象的-webView
。
然後,您可以實例化您的插件並存儲對WebView
的引用。
YourPluginView.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface YourPluginView : NSView <WebPlugInViewFactory>
{
WebView* webViewIvar;
}
- (id)initWithWebView:(WebView *)aWebView;
@end
YourPluginView.m:
//the WebPlugInViewFactory protocol required method
+ (NSView *)plugInViewWithArguments:(NSDictionary *)arguments
{
WebView* containerView = [[[arguments objectForKey:WebPlugInContainerKey] webFrame] webView];
YourPluginView* view = [[self alloc] initWithWebView:containerView];
return view;
}
- (id)initWithWebView:(WebView *)aWebView
{
self = [super init];
if(self)
{
webViewIvar = [aWebView retain];
}
return self;
}
- (void)dealloc
{
[webViewIvar release];
[super dealloc];
}
謝謝,這不正是我一直在尋找。 – Chris