user1249854
user1249854

Reputation: 187

Callback methods from C to Objective-C

I have an Objective-C class. What I am doing is I am calling C functions in my objective-C class [ This C functions I implemented in one file , which is part in this sample ios project]. All C functions are working fine , So far no issues.

When I try to call an asynchronous function in C file , that functions will give response to objective-c class after a while. I called function pointer in C which is triggered properly in Objective-C class. Please check the following code snippet. I want to do some UI related actions on the call back method. Is that possible ? If No is there any other way ? Could you please give me respons ASAP. Thanks.

C file :

void my_int_func(int x);

void test_asyn_fun ()
{
    /* This code is for Sleep logic */
    for (int i=0; i<50000; i++) {
        // sleep
    }
    /* Sleep End */
    void (*foo)(int);
    foo = &my_int_func;
    foo(25);
}  

Objective-C File:

void my_int_func(int x) // this is call back method 
{
    printf( "%d\n", x ); // working properly 
    [self anyMethodInMyClass]; // I am unable to use self in this function.
}

Actually My requirement is

I have C code which will do a Voip call functionality. I am calling C functions from my objective-C [iOS] code. If call has been disconnected by the receiver one of my C function is getting called and it stops the process. But still my UI is showing calling related UI. I want to dismiss that.

here , How do I send a notification to my objective-c class from C function to dismiss the UIView. Can any one kindly help me.

Actually i used function pointers but it's not working.

Upvotes: 3

Views: 2487

Answers (4)

user1249854
user1249854

Reputation: 187

First of all Thanks to Ricardo Kloth , Now it's working for me.

Here is my code ...

Objective-C Code:

static id staticObject = nil;

@Implementation MyObjCclass

init
{
    ....

    staticObject = self;
}

// C fun
void functionPointer() 
{ 
    [staticObject message]; // it's working 
}

-(void) message
{

}

@end

Upvotes: 2

r712m
r712m

Reputation: 329

I ran into this problem aswell. I assume you are using PJSIP for your VoIP app.

What you want to do is create a pointer to the Objective-C class you'd like to send messages to. You already figured you cannot call Objective-C functions from your C callback.

static YourObjCClass *objCClassPtr

@property (nonatomic, retain) YourObjCClass *class

When initializing said class, have the static pointer point to the Objective C object's pointer. (pointer to pointer to object)

objCClassPtr = class;

You are now able to send messages to the Objective-C object using [objCClassPtr message] from your C function as if you would write [class message]

You can point to self the same way.

Upvotes: 2

user1249854
user1249854

Reputation: 187

even in Blocks also you can't access instance variables right. Please check the following code snippet.

// this is simple block code

NSString* (^trimTheStr)(NSString*) = ^(NSString *str) {

[self myInstanceMethods]; // This will show error right 

NSString *result = nil;

result = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

return result;

};

Upvotes: 0

Ivan Dyachenko
Ivan Dyachenko

Reputation: 1348

You can use blocks. Blocks are available to pure C and C++, a block is also always an Objective-C object.

C++ Blocks Objects

Upvotes: 0

Related Questions