Re: Javascript call function Objective-C

2013-07-05 Thread Diederik Meijer | Ten Horses
I am not entirely sure whether this is as clean as it can be, but this is 
working for me. Might be a starting point, please give me feedback if this can 
be improved.. 

In your Objective-C class, put in appropriate method:

NSString *path = [[NSBundle mainBundle] 
pathForResource:@"javascriptFileName" ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path 
encoding:NSUTF8StringEncoding error:nil];
[self.myWebView stringByEvaluatingJavaScriptFromString:jsCode];
[self.myWebView 
stringByEvaluatingJavaScriptFromString:@"javascriptFunction()"];
NSString *someString = [self.myWebView 
stringByEvaluatingJavaScriptFromString:@"javascriptFunction()"];

In javascriptFileName.js file that has been added to the NSBundle:

function javascriptFunction() {
  // javascript logic here
return someReturnValue;
}

By the way, if you want to catch javascript click events, then I think you'd 
need to have that event trigger a return value. That way, your Objective-C 
class can interpret the javascript and act accordingly…



Op Jul 4, 2013, om 3:20 PM heeft Tibira Botto  het 
volgende geschreven:

> it is possible to access a javascript function in Objective-C?
> 
> didactic example:
> 
> - (NSString *) getName
> {
>   return @ "Test"
> }
> 
> in javascript
> function writeReturn(text)
> {
> document.write (text)
> }
> 
> onclick = writeReturn (getName () function that in Objective-C).
> 
> I'm using Webview and development for OSX.
> 
> thank you
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/diederik%40tenhorses.com
> 
> This email sent to diede...@tenhorses.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Javascript call function Objective-C

2013-07-04 Thread Jens Alfke

On Jul 4, 2013, at 6:20 AM, Tibira Botto  wrote:

> it is possible to access a javascript function in Objective-C?

From the example it looks like you mean “is it possible to call an Objective-C 
function from JavaScript”.

Look at  for how to do this. There are lots of 
comments that explain it.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Javascript call function Objective-C

2013-07-04 Thread Tibira Botto
it is possible to access a javascript function in Objective-C?

didactic example:

- (NSString *) getName
{
   return @ "Test"
}

in javascript
 function writeReturn(text)
{
 document.write (text)
}

onclick = writeReturn (getName () function that in Objective-C).

I'm using Webview and development for OSX.

thank you
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com