Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 27, 2012, 01:09 AM   #1
aruns1
macrumors newbie
 
Join Date: Mar 2012
Objective-C methods from Javascript in Cocoa

Hello!
I have a webview control in a Cocoa application where I am showing a web page. I need to access Objective-C method from Javascript in HTML page shown in Webview. I have tried "Calling Objective-C Methods From JavaScript"
from apple site "https://developer.apple.com/library/mac/#documentation/appleapplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html". But it did not worked. Pleases help me to Call Objective-C methods from Javascript.
aruns1 is offline   0 Reply With Quote
Old Mar 27, 2012, 01:52 AM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
The document from your post looks like it should get you where you're wanting to go. Post your code and tell us what happens. Is your exposed object undefined in javascript? Is it defined, but the methods you expect are not? Are the methods defined but there is some failure when you try to call them?

Post your code and a more complete description of its behavior and we'll be glad to help.

-Lee
lee1210 is offline   0 Reply With Quote
Old Mar 27, 2012, 02:35 AM   #3
aruns1
Thread Starter
macrumors newbie
 
Join Date: Mar 2012
Quote:
Originally Posted by lee1210 View Post
The document from your post looks like it should get you where you're wanting to go. Post your code and tell us what happens. Is your exposed object undefined in javascript? Is it defined, but the methods you expect are not? Are the methods defined but there is some failure when you try to call them?

Post your code and a more complete description of its behavior and we'll be glad to help.

-Lee
I have a class ObjCMethods as below
Code:
#import "ObjCMethods.h"

@implementation ObjCMethods

- (int)studentsInClass:(int)standard;
{
	return 20;
}
@end
with just one method as above. 

Also I am displaying an html page in webview as below:-

#import "Controller.h"
#import "ObjCMethods.h"
#import <WebKit/WebKit.h>

@implementation Controller
-(void)awakeFromNib
{
	
	
	[[_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"/Users/abcd/Desktop/sample.html"]]];
	ObjCMethods *record1 = [[ObjCMethods alloc] init];
	
	
	
	id win = [_webView windowScriptObject];
	
	[win setValue:record1 forKey:@"ObjCMethods"];
}

@end
_webview is outlet to Webview in the xib file.

The code for html page is as below:-
Code:
<html>
<body>
<script type="text/javascript">
var i=0;
while (i<=5)
  {
  document.write("The number is " + i);
  document.write("<br />");
  i++;
  }
var myrecord = window.ObjCMethods;
var studentsCount = studentsInClass_(5);
document.write(studentsCount);
</script>
</body>
</html>
I expect output to be
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
20
But at bootom 20 is missing from the original output showing that Objective-C method is not being called.

Last edited by maflynn; Mar 27, 2012 at 10:45 AM. Reason: code tags added
aruns1 is offline   0 Reply With Quote
Old Mar 27, 2012, 06:31 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Try calling studentsInClass_ on myrecord in your JavaScript, rather than treating it as a global method.

-Lee
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Call Cocoa function from Javascript mfniaki Mac Applications and Mac App Store 0 Sep 8, 2011 10:25 AM
Missing Tracks from iTunes in the Cloud AndrewLGP Mac Applications and Mac App Store 3 Jun 19, 2011 03:16 PM
call method from subclass of uiimageview sregorcinimod iPhone/iPad Programming 3 Apr 22, 2011 11:54 PM
Call method from AppController in another class gonche1124 Mac Programming 2 Sep 13, 2009 08:29 AM
How to run applescript from javascript in MAC vinothjaguva Mac Applications and Mac App Store 0 May 21, 2008 12:52 AM


All times are GMT -5. The time now is 02:04 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC