• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Using Objective-C From JavaScript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using Objective-C From JavaScript


  • Subject: Re: Using Objective-C From JavaScript
  • From: Todd Ditchendorf <email@hidden>
  • Date: Fri, 11 Nov 2005 17:03:27 -0600

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
References: 
 >Using Objective-C From JavaScript (From: Chad Rosenberg <email@hidden>)

  • Prev by Date: Re: Overriding HTML translation in NSAttributedString
  • Next by Date: NSManagedObject and NSCoding
  • Previous by thread: Using Objective-C From JavaScript
  • Next by thread: Re: Using Objective-C From JavaScript
  • Index(es):
    • Date
    • Thread