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.

4
  • 4
    How do you expect the presence of self outside the class? Commented Aug 30, 2012 at 9: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 Commented Aug 30, 2012 at 9:38
  • do you know what the self represents? Commented Aug 30, 2012 at 9:43
  • I think this is should help you [Pass a block to a C++ method from objective C][1] [1]: stackoverflow.com/questions/6014201/… Commented Aug 30, 2012 at 9:44

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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;

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.