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
You can use blocks. Blocks are available to pure C and C++, a block is also always an Objective-C object.
C++ Blocks Objects
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
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;
};
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.