3

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.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
user1249854
  • 187
  • 1
  • 8
  • 4
    How do you expect the presence of `self` outside the class? –  Aug 30 '12 at 09:36
  • Can we have full (or at least more) source code please? Most likely cause is that you are using `self` in a class rather than object method. When calling a method on a object you need an object pointer from somewhere - I don't see how you're picking one up in your C file – marko Aug 30 '12 at 09:38
  • do you know what the `self` represents? – holex Aug 30 '12 at 09:43
  • I think this is should help you [Pass a block to a C++ method from objective C][1] [1]: http://stackoverflow.com/questions/6014201/pass-a-block-to-a-c-method-from-objective-c – Ivan Dyachenko Aug 30 '12 at 09:44

4 Answers4

2

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.

r712m
  • 299
  • 3
  • 7
2

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
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
user1249854
  • 187
  • 1
  • 8
0

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

C++ Blocks Objects

Ivan Dyachenko
  • 1,348
  • 9
  • 16
0

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;

};

user1249854
  • 187
  • 1
  • 8