Calling an Objective-C method from Javascript - javascript

I am new to iOS and I am not getting how to call an objectiveC method from Javascript using web view.
I have already tried this but it didn't work. Additionally, please help me with my objectiveC code.
function IosSuccess(transactionid)
{
alert("Transaction successful,transaction id :" +transactionid);
var ret = jsb.reflection.callStaticMethod("PaymentViewController","TransactionId:",""+transactionid);
}

You cannot call objective c method using JavaScript directly rather you have to use JavaScriptCore.framework and making an interface for call back will do the required task.

Related

Calling Java function from Rhino

Calling Javascript functions running inside Rhino from Java is easy enough - that after all is why Rhino was created. The thing I am having trouble establishing is this:
Context: I have a Phonegap CLI (v 6.3.3) Android project (API 19+) where I do a great deal of processing via loadable JavaScript running inside rhino
A Phonegap plugin - which I am creating at the same time as the actual Phonegap app - contains class called Storage which provides public, static, methods such as readFromFile(String fileName), writeToFile(String fileName,String data) etc.
What I want to be able to do is to call Storage.readFromFile etc from my loaded JavaScript code in Rhino.
Just how this should be done is not too clear to me. From the searches I have done thus far it involves using ScriptableObject.putProperty to pass the Java class in question, Storage in my case to JavaScript. However, how this should be done and then how it should be used at the JS end leaves me rather confused.
I would be most grateful to anyone here who might be able to point me in the right direction
Given that Rhino has less than 100 followers here it should perhaps come as little surprise that this question was not answered. In the mean time I have managed to find the solution myself and it turns out to be very simple. I share it below for the benefit of anyone else running into this thread.
My Storage class is very simple. It goes something like this
public class Storage
{
public static boolean haveFile(){}
public static boolean readFromFile(String fname){}
...
}
When I call Javascript from Java via Rhino I simply pass a new instance of the Storage class as the last of my function parameters
Context rhino = Context.enter();
Object[] functionParams = new Object[] {"Other parameters",new Storage()};
rhino.setOptimizationLevel(-1);
try
{
Scriptable scope = rhino.initStandardObjects();
String rhinoLog = "var log = Packages.io.vec.ScriptAPI.log;";
String code = /*Javascript code here* as shown separately below/;
rhino.evaluateString(scope, rhinoLog + code, "ScriptAPI", 1, null);
Function function = (Function) scope.get("jsFunction", scope);
Object jsResult = function.call(rhino,scope,scope,functionParams);
}
where the Javascript code is
function jsFunction(a,s)
{
//a - or a,b,c etc - here will be the "other" parameters
//s - will be the instance of the Java side Storage class passed above
//now you can do things like
s.writeToFile('fileName','fileData');
var fd = s.readFromFile('fileName');
s.dropFile('fileName');
...
}

how to call dlls from java script

Can any one please help me how to call dll functions from javascript. while using activexobject I am getting error "automation server cannot create object". Here is my code
var jMyAcctId = document.all.RefNum.value;
var jMyAcctType = document.all.TrxType.value;
var NewObject = new ActiveXObject("HDMFCDV.cdv");
if (NewObject.IsValidID(jMyAcctId,jMyAcctType) == true)
{
document.all.RefNumError.innerText = "";
CnvUp(sel);
document.all.CustFName.disabled = false;
document.all.CustFName.focus();
}
Thanak in advance.
Your JavaScript code is good. I suspect the problem is with the HDMFCDV.cdv ActiveX - either they way you implemented it or they way you registered it.
I'm not familiar with HDMFCDV object. Is that a proprietary object you implemented? Here are few tips to troubleshoot:
Make sure your object is registered (did you run regsrv32?)
Verify HDMFCDF.cdv is in the registry: HKCR\HDMFCDF.cdv
Make sure there is a CLSID
Make sure the class ID is in the registry, and that it points to the DLL implementing your object. HKCR\CLSID{your-guide}\InprocServer32 (REG_SZ)
A very common lookout: have you implemented IObjectSafety. Without this interface, and without this interface returning that it is safe for untrusted caller, IE will refuse to instantiate this object

AS3 - ExternalInterface.addCallback, Access of undefined property

I am trying to use javascript to run AS3 functions. When I attempt to compile I'm getting an "Access of undefined property" error message.
I've read a few things online about this but I'm still not understanding it. I want to have the flash file always listening for javascript.
Here is my AS3 code:
ExternalInterface.addCallback("song4", PauseMusicExt);
And my Javascript & HTML:
function returnVar3(song3) { return this[song3]; }
<input type="submit" name="playButton" id="playButton" value="Submit" onClick="returnVar('song3')"/>
Edit: Here is the pauseMusic function:
function pauseMusicExt():void
{
songPosition = channel.position;
channelSilence.stop();
channel.stop();
channel2.stop();
btnPlay.mouseEnabled = true;
}
I'm not sure about the extend of your app but you've got your addCallback function parameters mixed up..
See the doc, the first parameter is the name you want to expose the function as to javascript, the second is the actual internal AS3 function you want to trigger.
So the declaration should likely be something like:
ExternalInterface.addCallback("song4", pauseMusic);
(assuming that your function in the same scope as where you call addCallback)
That statement will create a "song4" method that you can call on your flash dom object
var fl = document.getElementById('myflashobject');
fl.song4()
After there's the issue that pauseMusic want a parameter (looks like you've made it a mouse event handler). You probably want to have a clean method that doesn't require a parameter like an internal as3 event param. Rewrite pauseMusic so it doesn't require it (you might need to create another method to handle the mouse event internally - like onPause(evt:MouseEvent), which then calls pauseMusic.
Edit: I don't know if a lot of people thought about doing that, but you can also totally use external interface to call firebug's console.log function to send messages to Firebug from flash (it's really helpful for debugging ExternalInterface issues, or any other flash problems - see the ExternalInterface.call function)
Hope u want to pause the audio player.
AS code :
ExternalInterface.addCallback("sndToAS",rcvdFmJS);
function rcvdFmJS(val){
if (val == "pause"){
audioPause();
}
}
JS code :
document.getElementById("movie").sndToAS("pause");

Registering a javascript method in GWT java code so that it can be invoked based on an event in GWT

I tried looking for a similar post but couldn't find any, hence posting this query for your help. Essentially, I have a custom UI created on the GWT side. Now, I want to send events occuring at the GWT side over to the javascript/jsp page. For this I was wondering if there's a way for the jsp/javascript to register a method in the GWT code, and whenever, any event happens at the GWT side, the GWT java code can simply invoke this javascript method (which is like a function pointer/object), and the information would be notified at the jsp page. Though I can directly call javascript methods from within the GWT code, however, that means that the GWT code also need to know the javascript method name, and this results in a tight coupling. Instead the javascript could simply pass in a handle to the function to the GWT code, which would simply invoke this handle to pass in necessary events on the jsp/javascript code. Any ideas would be very helpful.
You can use a Dictionary to pass the function name to your application.
In the JSP host page:
<script type="text/javascript">
function myFunction() {
// Do stuff...
}
var MyDictionary = {
myCallback = "myFunction"
};
</script>
And in your application:
public void onEvent(EventType event) {
Dictionary d = Dictionary.getDictionary("MyDictionary");
invokeNativeCallback(d.get("myCallback"));
}
private native void invokeNativeCallback(String callbackName) /*-{
if (typeof $wnd[callbackName] === "function") {
$wnd[callbackName]();
}
}-*/;

How to call javascript from applet without use showDocument?

I'm using
getAppletContext().showDocument(new URL("javascript:" + command));
to call javascript from applet.
But sometimes in firefox this doen't work, I don't know why.
So, now I'm trying to use JSObject, using this:
JSObject jsObject = new JSObject();
jsObject.eval(command);
But I got this error:
Exception in thread "thread applet-com.foo.bar.TestApplet-6"
java.lang.InstantiationError: netscape.javascript.JSObject
Why I'm getting this error?
There is another way to do java applet to javascript communication?
I'm using
JSObject jsObject = JSObject.getWindow(this);
jsObject.eval(....);
And it works now.

Categories