Re: Using Objective-C From JavaScript
Re: Using Objective-C From JavaScript
- Subject: Re: Using Objective-C From JavaScript
- From: Chad Rosenberg <email@hidden>
- Date: Fri, 11 Nov 2005 15:49:43 -0800 (PST)
Thanks. I gave it a try and it produced the same results. To clarify,
here's the html I am testing it against:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function printNameAtIndex(index) {
document.write("<html><body>");
try {
var myaddressbook = window.AddressBook;
//var name = myaddressbook.nameAtIndex_(index);
document.write(typeof(myaddressbook));
document.write(" >")
document.write(name)
document.write("<")
}
catch (e) {
document.write(e)
}
document.write("</body></html>");
}
</SCRIPT>
</head>
<body bgcolor="#ffffff">
<form action="dummy" method="post">
<input type="button" value="AddressBook" name="B1" onClick="printNameAtIndex(1)">
</form>
</body>
</html>
===
It is the line with document.write(typeof(myaddressbook)) that
reports addressbook is a "function" so there's no way I can
execute the code myaddressbook.nameAtIndex_(index);.
This is the error that is produced if I run var name =
myaddressbook.nameAtIndex_(index):
TypeError - Value undefined (result of expression
myaddressbook.nameAtIndex_) is not object.
[ windowScriptObject setValue:littleBlackBook forKey:@"AddressBook"] does
not seem to be properly passing littleBlackBook in as JavaScript
object called AddressBook. Is this a bug in WebScriptObject, or am I
missing some hoops? (I'm assuming the latter.) Are there any
methods that BasicAddressBook is required to implement inorder to show up
as an Object in the JavaScript? I've tried implementing
webScriptNameForSelector mentioned in the example, but it didn't help.
Thanks again,
Chad
On Fri, 11 Nov 2005, Todd Ditchendorf wrote:
Chad, If I'm understanding you correctly, I came across the EXACT same
problem a couple of months ago when I started with WebKit. This baffled me
for hours.
Here's the solution.
You should not try to get a reference to webView's windowScriptObject
immediately after instantiating webView. You cannot yet set references to
your ObjC objects on that windowScriptObject immediately.
Instead, you have to register a WebFrameLoadDelegate on the WebView, and wait
for the following callback method to add your reference to your ObjC object
to JavaScript:
- (void)webView:(WebView *)sender windowScriptObjectAvailable:
(WebScriptObject *)windowScriptObject
So your code would look something like this:
- (void)awakeFromNib
{
[webView setFrameLoadDelegate:self];
NSString* url = @"file:///tmp/test.html";
[[webView mainFrame] loadRequest:[NSURLRequest
requestWithURL:[NSURL URLWithString:url]]];
}
- (void)webView:(WebView *)sender windowScriptObjectAvailable:
(WebScriptObject *)windowScriptObject
{ BasicAddressBook *littleBlackBook = [BasicAddressBook
addressBook];
[ windowScriptObject setValue:littleBlackBook forKey:@"AddressBook"];
}
On Nov 10, 2005, at 1:59 PM, Chad Rosenberg wrote:
I was trying to follow the "Using Objective-C From JavaScript" example on
http://developer.apple.com/documentation/
AppleApplications/Conceptual/SafariJSProgTopics/Tasks/
ObjCFromJavaScript.html and I can't seem to get it to work. When I run the
following:
function printNameAtIndex(index) {
var myaddressbook = window.AddressBook;
var name = myaddressbook.nameAtIndex_(index);
document.write(name);
}
typeof(window.AddressBook) returns "function" and
myaddressbook.nameAtIndex_ is undefinied.
I set up the AddressBook in an awakeFromNib method:
- (void)awakeFromNib
{
BasicAddressBook *littleBlackBook = [BasicAddressBook addressBook];
id win = [webView windowScriptObject];
[win setValue:littleBlackBook forKey:@"AddressBook"];
NSString* url = @"file:///tmp/test.html";
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL
URLWithString:url]]];
NSLog(@"awake");
}
The above properly displays the test.html, and
[win setValue:littleBlackBook forKey:@"AddressBook"] seems to doing
something, since without it "window.AddressBook" becomes undefined in the
Java Script.
Any help would be appriciated.
Thanks,
Chad
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden