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

Similar Threads
thread Thread Starter Forum Replies Last Post
Calling Objective-C from Javascript Blakeasd Mac Programming 8 Mar 18, 2014 08:41 AM
Cocoa Touch and Objective C phalgun27 iPhone/iPad Programming 7 Aug 1, 2013 07:09 PM
Basic Objective-C Cocoa questions? cpuin Mac Programming 8 Jun 3, 2013 07:10 PM
HTML5 and JavaScript wrapped in Cocoa cpuin Mac Programming 2 May 18, 2013 03:03 PM
WebView in Cocoa (Objective-C) tr!pf!3 Mac Programming 0 Aug 28, 2012 09:48 AM

Forum Jump

All times are GMT -5. The time now is 05:34 PM.

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

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