1 decade ago
by Aitor
Hello there.
I'm porting a game made with Impact to the iphone with IOSImpact, and I wanted to know if there is a way to acces cocoa classes directly from the js files.
Thanks in advance
1 decade ago
by Aitor
I've read those articles, but I still don't know how to do it. Could someone please post an example that shows how to call an Objective-c method from javascript? I've looked into the IOSImpact source, but It looks too complex and I can't find anything.
And one last question: can I for example use IAds on an IOSImpact app?
1 decade ago
by dominic
iOSImpact actually ships with an
iAd
class, it's just not documented. Use it like this:
// Create the AdBanner object before calling ig.main.
// Pass 'true' for displaying at the bottom, 'false' for top
ig.adBanner = new native.AdBanner( false );
ig.main( ... );
You can then use
ig.adBanner.show()
and
ig.adBanner.hide()
. Note that the Banner will only show when it is completely loaded. It will not display when there's no Internet connection.
You can also expose your own Objective-C classes to JavaScript. Just derive them from
JS_BaseClass
and expose functions with the
JS_FUNC()
macro. An instance of such a class can be created in JavaScript with
var instance = new native.MyClassName();
Have a look at
Classes/Impact/JS_LocalStorage.*
and the
game/ios-impact.js
line 7 for a simple example.
edit: i did not use iOSImpact yet, but here my way to access cocoa from javascript.
if you want to access cocoa methods, you need to prepare your webview and interrupt the request in the shouldStartLoadWithRequest delegate method
doc
example:
in js you call document.location = "myIdentifier:methodName.param"
cocoa will call shouldStartLoadWithRequest.
with the request and his url you can access the string above. split at ":" and if you see your identifier, return NO - so the request is canceled and the webview does not load anything.
at this point you can parse the rest of your url and call the methods depending on your javascript
1 decade ago
by Aitor
Thanks for the answer dominic, that's really great!
1 decade ago
by Aitor
I have another little question about this, but I don't think it's worth a new thread.
How can I make a Objective c method return a javascript string? I want to get a JSON string from a URL in a objective c method, and use the return value to make a javascript object with JSON.stringify(). So how can I do this?
I've seen you use this code in JS_LocalStorage:
JSStringRef jsValue = JSStringCreateWithUTF8CString( [value UTF8String] );
JSValueRef ret = JSValueMakeString( ctx, jsValue );
JSStringRelease( jsValue );
But i can't test it right now (I don't have access to a mac ). Is this the right way to do it?
Page 1 of 1
« first
« previous
next ›
last »