how to call cocos2d-js method from objective-c - objective-c

i am using cocos2d-js , that's a in purchase app,when purchase success ,i need to notify the message to cocos2d-js to update ui or something else,
i know call objective-c from cocos2d-js is like:
in js:
jsb.reflection.callStaticMethod("objective-cClass","methodName:", "parm");
but,how to call cocos2d-js from objective-c...

May it help someone, as we know, objective-c can easily call c++ methods, so we can use like this:
jsval ret;
ScriptingCore::getInstance()->evalString("CommonFun.setDiamond(88)", &ret);
and dont forget include head file, for cocos2d-x 3.8:
#include "cocos/scripting/js-bindings/manual/ScriptingCore.h"

1:how js call objc methods like this, js call only static method
(1)//NativeOcClass.mm filename
import <Foundation/Foundation.h>
#interface NativeOcClass : NSObject
+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content;
#end
#implement NativeOcClass
+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alertView show];
return true;
}
#end
(2)for js file
var ret = jsb.reflection.callStaticMethod("NativeOcClass",
"callNativeUIWithTitle:andContent:",
"cocos2d-js",
"Yes! you call a Native UI from Reflection");
2:objc call js method
(1)js method,e.g.//Test.js
var Test={
myMethod:function(){
console.log("call js");
}
}
(2)objc //filename.mm
#include "cocos/scripting/js-bindings/manual/ScriptingCore.h"
....
-(void)callJsMethod
{
jsval ret;
ScriptingCore::getInstance()->evalString("Test.myMethod()", &ret);
}

Related

CNUI ERROR Selection predicates are set but the delegate does not implement contactPicker:didSelectContact:

I try to use the new iOS 9.0 CNContactPickerViewController to select a contact in objective-C. I set the delegate and implement the CNContactPickerDelegate methods.
#import ContactsUI;
#import Contacts;
//-----------------------------------------------------------------------
- (void) presentContacts
{
CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];
contactPicker.delegate = self;
contactPicker.predicateForEnablingContact = [NSPredicate predicateWithFormat:#"familyName LIKE[cd] 'smith'"];
contactPicker.predicateForSelectionOfContact = [NSPredicate predicateWithFormat:#"TRUEPREDICATE"];
[_myViewController presentViewController:contactPicker animated:YES completion:nil];
}
//-----------------------------------------------------------------------
- (void) contactPickerDidCancel: (CNContactPickerViewController *) picker
{
NSLog(#"didCancel");
}
//-----------------------------------------------------------------------
- (void) contactPicker: (CNContactPickerViewController *) picker
didSelectContact: (CNContact *) contact
{
NSLog(#"didSelectContact"):
}
//-----------------------------------------------------------------------
- (void) contactPicker: (CNContactPickerViewController *) picker
didSelectContactProperty: (CNContactProperty *) contactProperty
{
NSLog(#"didSelectProperty");
}
//-----------------------------------------------------------------------
The contacts picker is presented with 'smith' selectable but I get the following message:
[CNUI ERROR] Selection predicates are set but the delegate does not implement contactPicker:didSelectContact: and contactPicker:didSelectContactProperty:. Those predicates will be ignored.
And I never get any log from the delegate methods. It behaves exactly as the line
contactPicker.delegate = self;
is ignored. Even I click on the "cancel" button in the picker, I don't get my "didCancel" message but I get another message:
plugin com.apple.MobileAddressBook.ContactsViewService invalidated
I found in https://forums.developer.apple.com/thread/12275 somebody with the similar problem in swift and he solved it telling us: "So I found that the ContactsPicker I was calling was in the wrong module" but I do not understand how we can get the wrong module and how to call the "right" module.
I have the same problem on the simulator and on a real device (iPad).
Thanks to Joel in my related question With CNContactPickerViewController in iOS 9.0, how to enable/disable single or multiple selection?, I found that I just forgot to store the CNContactPickerViewController in a property that exists the time the user make the selection.
So my code becomes:
- (void) presentContacts
{
_contactPicker = [[CNContactPickerViewController alloc] init];
contactPicker.delegate = self;
...
}

objective c to swift blocks

I'm having trouble converting this objective C to swift blocks
you can see here Code on Github, but i don't have any background in objective C, so i don't really understand this block, here is the code
- (LinkedStringTapHandler)exampleHandlerWithTitle:(NSString *)title
{
LinkedStringTapHandler exampleHandler = ^(NSString *linkedString) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title
message:[NSString stringWithFormat:#"Handle tap in linked string '%#'",linkedString]
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil, nil];
[alert show];
};
return exampleHandler;
}
you can see this code on github https://github.com/evilBird/HBVLinkedTextView/blob/master/HBVLinkedTextViewExample/HBVLinkedTextViewExample/ViewController.m , take a look at that code, Im trying to use it in swift using bridging header. Everything i converted but this line i dont understand.
LinkedStringTapHandler exampleHandler = ^(NSString *linkedString)
this is what i have done so far
func exampleHandlerWithTitler(title:NSString)->LinkedStringTapHandler {
exampleHandler: LinkedStringTapHandler = (linkedString:NSString) -> () {
// alert view code here
}
return exampleHandler (ERROR here Use of unresolved identifier exampleHandler)
}
Where Objective-C has blocks, Swift has functions. So it's just a matter of knowing how to read Objective-C block syntax - not easy, I know:
^(NSString *linkedString)
That means: "This is a block taking an NSString parameter (and returning nothing)." So you want to supply here a Swift function that takes an NSString parameter (and returns nothing).
In other words, the definition of LinkedStringTapHandler is equivalent to saying this in Swift:
typealias LinkedStringTapHandler = (linkedString:NSString) -> ()
So in your code you would form a function of that type and return it:
func f(linkedString:NSString) -> () {
// do stuff
}
return f
You could do the same thing with an anonymous function but that's neither here nor there, really.
finally this is working
func exampleHandlerWithTitler(title:NSString)->LinkedStringTapHandler {
let exampleHandler: LinkedStringTapHandler = {(linkedString:String) in {
// alert view code here
}
return exampleHandler
}

Why is the following UIAlertView code not working

I'm trying to get the following working, this example appears on a few websites but i just can't seem to get it working.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Yes, like this" message:#"What are you looking at?" cancelButtonTitle:#"Leave me alone" otherButtonTitles:#"Button 1",#"Button 2",nil];
[alert showWithDismissHandler:^(NSInteger selectedIndex, BOOL didCancel) {
if (didCancel) {
NSLog(#"User cancelled");
return;
}
switch (selectedIndex) {
case 1:
NSLog(#"1 selected");
break;
case 2:
NSLog(#"2 selected");
break;
default:
break;
}
}];
The warnings I'm getting are
No visible #interface for 'UIAlertView' declares the selector 'initWithTitle:message:cancelButtonTitle:otherButtonTitles:'
No visible #interface for 'UIAlertView' declares the selector 'showWithDismissHandler:'
Properly a really daft question but what am i missing.
Thanks
The signature is not correct. You missed delegate.
– initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:
The standard UIAlertView does not have showWithDismissHandler method. If you copied some code from internet, likely you will need to download some third party package that supports UIAlertView with block callback (there are quite a few of them).
Yes, Peter is right! The first warning is because the signature is not correct:
– initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:
and what you trying to do here is to do something when the alertview is dismissed and the method you are using is not available. Implement UIAlertViewDelegate and then use any of these methods to do what you are trying to do here.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
Here's the link to UIAlertView delegate reference:
http://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html
Are you using something like UBAlertView, by any chance? If so, you need to instantiate a UBAlertView, not a UIAlertView:
UBAlertView *alert = [[UBAlertView alloc] initWithTitle:#"Yes, like this"
message:#"What are you looking at?"
cancelButtonTitle:#"Leave me alone"
otherButtonTitles:#"Button 1",#"Button 2",nil];
[alert showWithDismissHandler:^(NSInteger selectedIndex, BOOL didCancel) {
... etc ...
(I like to put line breaks in long method calls to make them more readable.)

NSAlert+Blocks - Need assistance setting up new extension

I'm trying to do an NSAlert using blocks. (I want the last button added to be a cancel button... see below) When I try to call it in my code, I get an error saying an unrecognized selector is being sent to the class. Did I setup my extension improperly?
Here is my call:
[NSAlert showSheetModalForWindow:self.window
WithTitle:#"Allow Sync"
message:message
informativeText:#"Data entered into Easy Spend Log will be shared."
alertStyle:NSWarningAlertStyle
cancelButtonTitle:#"Don't Allow"
otherButtonTitles:#[#"Allow",#"Always Allow"]
onDismiss:^(int buttonIndex) {
if (buttonIndex == 0)
[self.syncEngine allowSync:YES alwaysAllow:NO forConnection:connection];
else
[self.syncEngine allowSync:YES alwaysAllow:YES forConnection:connection];
}
onCancel:^ {
[self.syncEngine allowSync:NO alwaysAllow:NO forConnection:connection];
}];
NSAlert+Blocks.h
#import <Foundation/Foundation.h>
typedef void (^DismissBlock)(int buttonIndex);
typedef void (^CancelBlock)();
#interface NSAlert (Blocks)
+ (NSAlert*) showSheetModalForWindow:(NSWindow*) window
WithTitle:(NSString*) title
message:(NSString*) message
informativeText:(NSString*) text
alertStyle:(NSAlertStyle) style
cancelButtonTitle:(NSString*) cancelButtonTitle
otherButtonTitles:(NSArray*) otherButtons
onDismiss:(DismissBlock) dismissed
onCancel:(CancelBlock) cancelled;
#end
NSAlert+Blocks.m
#import "NSAlert+Blocks.h"
static DismissBlock _dismissBlock;
static CancelBlock _cancelBlock;
#implementation NSAlert (Blocks)
+ (NSAlert*) showSheetModalForWindow:(NSWindow*) window
WithTitle:(NSString*) title
message:(NSString*) message
informativeText:(NSString*) text
alertStyle:(NSAlertStyle) style
cancelButtonTitle:(NSString*) cancelButtonTitle
otherButtonTitles:(NSArray*) otherButtons
onDismiss:(DismissBlock) dismissed
onCancel:(CancelBlock) cancelled {
_cancelBlock = [cancelled copy];
_dismissBlock = [dismissed copy];
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:message];
[alert setInformativeText:text];
[alert setAlertStyle:style];
for(NSString *buttonTitle in otherButtons)
[alert addButtonWithTitle:buttonTitle];
[alert addButtonWithTitle:cancelButtonTitle];
[alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:#selector(alertDidEnd:returnCode:contextInfo:)];
return alert;
}
+ (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
if(returnCode == [alert [buttons count]-1])
{
_cancelBlock();
}
else
{
_dismissBlock(returnCode - 1); // cancel button is button 0
}
}
#end
Once I setup a test project, the problem became evident. The call to the alert in the category .m file was wrong.
I have posted the working code onto github. https://github.com/AaronBratcher/NSAlert-Blocks

iPhone SDK - Call Objective C function

i would like to create a function called "playSound" with one (but later two parameters - one, which is the filename and one, which is the musictype (mp3,...))
but i cant get the paramter working in my function...please help me
here's my code
//
// MainView.m
//
// Created by Christopher Fuchs on 26.01.11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MainView.h"
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVFoundation.h>
#implementation MainView
void playSound(char filename){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mein Alert" message:filename delegate:nil cancelButtonTitle:#"Abbrechen" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (IBAction)PlayFatGuyWalking:(id)sender {
playSound(#"MusicFile");
}
#end
For now, i just added an alert instead of the playing sound framework
the paramater is called "filename" and should be called as message of the alert
thanks in advance
greez
- (void)playSound:(NSString *)filename {
// method body
}
// calling
[self playSound:#"MusicFile"];
Make this line:
void playSound(char filename)
into this:
void playSound(NSString *filename)
The # "foo" construction says "this is an NSString".
Also, if you want to keep it a C function, you might want to move it out of the #implementation block.
You will find that virtually everything asking for a string in Cocoa will want an NSString, and not a char *.
Oh yeah, and the (char filename) won't work in plain C either, that a single byte variable, not a pointer to a potential string.