Re: Why the Cocoa function can't be called from JavaScript?
Re: Why the Cocoa function can't be called from JavaScript?
- Subject: Re: Why the Cocoa function can't be called from JavaScript?
- From: Rob Keniger <email@hidden>
- Date: Tue, 15 Sep 2009 11:09:42 +1000
On 14/09/2009, at 1:41 PM, ziqian zhan wrote:
When first time welcome.html is loaded into webView from
-(void)awakeFromNib;, push the "change it" button on web page the
-(NSString*)getName function is called. The "hello world" is
replaced with
"hi I'm here.". But when welcome.html loaded again from
-(IBAction)loadPage:(id)sender, push the "change it" button on web
page but
NOTHING happens. The "hello world" is no way changed to "hi I'm
here.". It
looks that the JSBridge.getName() disappeared.
This is happening because you're setting the value of the JSBridge
object in the windowScriptObject before you've even loaded the page:
NSString * path = [[NSBundle mainBundle] pathForResource:@"welcome"
ofType:@
"html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL
fileURLWithPath:path]]];
The windowScriptObject is not valid before the page loads, and then
when the page loads you are not registering the JSBridge object with
the window.
You need to set your object as the WebFrameLoadDelegate and register
your JSBridge object in the delegate method -
webView:didClearWindowObject:forFrame:, which is called when the
windowScriptObject becomes available:
- (void)webView:(WebView *)sender didClearWindowObject:
(WebScriptObject*)windowObject forFrame:(WebFrame *)frame
{
[windowObject setValue:self forKey:@"JSBridge"];
}
One other thing I noticed is that you are allowing access to all
Objective-C methods in your app by doing this:
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; }
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name { return NO; }
This is a massive security hole, you should only allow keys and
selectors that you know to be "safe" to return NO from these methods.
--
Rob Keniger
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden