• 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: Javascript call function Objective-C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Javascript call function Objective-C


  • Subject: Re: Javascript call function Objective-C
  • From: Diederik Meijer | Ten Horses <email@hidden>
  • Date: Fri, 05 Jul 2013 12:39:23 +0200

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 <email@hidden> 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 (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

_______________________________________________

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


References: 
 >Javascript call function Objective-C (From: Tibira Botto <email@hidden>)

  • Prev by Date: Re: Post to a particular friend or friends group on Facebook
  • Next by Date: The cost of using objects rather than plain C variables
  • Previous by thread: Re: Javascript call function Objective-C
  • Next by thread: The cost of using objects rather than plain C variables
  • Index(es):
    • Date
    • Thread