使objective-C函数的地址等于C函数的指针

使objective-C函数的地址等于C函数的指针,objective-c,c,callback,pjsip,Objective C,C,Callback,Pjsip,我希望objective-C函数的地址等于指向C函数的指针 我可以让它与下面的C函数一起工作,但如果可能的话,我只想使用objective-C //member function from type pjsua_callback (cfg.cb) void(* on_call_state )(pjsua_call_id call_id, pjsip_event *e) //Initialize the applications configuration callbacks app_confi

我希望objective-C函数的地址等于指向C函数的指针

我可以让它与下面的C函数一起工作,但如果可能的话,我只想使用objective-C

//member function from type pjsua_callback (cfg.cb)
void(* on_call_state )(pjsua_call_id call_id, pjsip_event *e)

//Initialize the applications configuration callbacks
app_config->cfg.cb.on_call_state = &on_call_state;

我想用objective-c函数来实现这一点

//objective-c function attempting to recreate the c function
- (void)on_call_state:(pjsua_call_id )call_id andEvent:(pjsip_event *)e{
    NSLog(@"on_call_state, call_id = %d", call_id);
    pjsua_call_info ci;
    pjsua_call_get_info(call_id, &ci);
    [self postCallStateNotification:call_id andCallInfo:&ci];

}
cfg.cb.on_call_state = &[self on_call_state:andEvent:];
为什么我不能让objective-c函数像c函数一样返回(void)并像c函数那样初始化回调(没有c函数中的参数)


我希望cfg.cb.on_call_状态等于我的objective-c函数的地址

最后,我从自己的问题中找到了解决方案。对于那些仍在寻找解决方案的人来说,解决方案是让自己的C文件保存回调逻辑,并将C文件添加到objective-C代码中

//objective-c function attempting to recreate the c function
- (void)on_call_state:(pjsua_call_id )call_id andEvent:(pjsip_event *)e{
    NSLog(@"on_call_state, call_id = %d", call_id);
    pjsua_call_info ci;
    pjsua_call_get_info(call_id, &ci);
    [self postCallStateNotification:call_id andCallInfo:&ci];

}
cfg.cb.on_call_state = &[self on_call_state:andEvent:];
因此,您的C文件(头文件)将如下所示

//
//  my_app.h
//  AnuranRealSIPOBJ
//
//  Created by Anuran Barman on 01/07/19.
//  Copyright © 2019 Anuran Barman. All rights reserved.
//

#ifndef my_app_h
#define my_app_h

#include <stdio.h>
#import <pjsua.h>

#define THIS_FILE "APP"

static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
                             pjsip_rx_data *rdata) {
    pjsua_call_info ci;

    PJ_UNUSED_ARG(acc_id);
    PJ_UNUSED_ARG(rdata);

    pjsua_call_get_info(call_id, &ci);

    PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
              (int)ci.remote_info.slen,
              ci.remote_info.ptr));

    /* Automatically answer incoming calls with 200/OK */
    pjsua_call_answer(call_id, 200, NULL, NULL);
}

/* Callback called by the library when call's state has changed */
static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
    pjsua_call_info ci;

    PJ_UNUSED_ARG(e);

    pjsua_call_get_info(call_id, &ci);
    PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
              (int)ci.state_text.slen,
              ci.state_text.ptr));
}

/* Callback called by the library when call's media state has changed */
static void on_call_media_state(pjsua_call_id call_id)
{
    pjsua_call_info ci;

    pjsua_call_get_info(call_id, &ci);

    if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
        // When media is active, connect call to sound device.
        pjsua_conf_connect(ci.conf_slot, 0);
        pjsua_conf_connect(0, ci.conf_slot);
    }
}

#endif /* my_app_h */

最后,我从自己的问题中找到了解决办法。对于那些仍在寻找解决方案的人来说,解决方案是让自己的C文件保存回调逻辑,并将C文件添加到objective-C代码中

因此,您的C文件(头文件)将如下所示

//
//  my_app.h
//  AnuranRealSIPOBJ
//
//  Created by Anuran Barman on 01/07/19.
//  Copyright © 2019 Anuran Barman. All rights reserved.
//

#ifndef my_app_h
#define my_app_h

#include <stdio.h>
#import <pjsua.h>

#define THIS_FILE "APP"

static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
                             pjsip_rx_data *rdata) {
    pjsua_call_info ci;

    PJ_UNUSED_ARG(acc_id);
    PJ_UNUSED_ARG(rdata);

    pjsua_call_get_info(call_id, &ci);

    PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
              (int)ci.remote_info.slen,
              ci.remote_info.ptr));

    /* Automatically answer incoming calls with 200/OK */
    pjsua_call_answer(call_id, 200, NULL, NULL);
}

/* Callback called by the library when call's state has changed */
static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
    pjsua_call_info ci;

    PJ_UNUSED_ARG(e);

    pjsua_call_get_info(call_id, &ci);
    PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
              (int)ci.state_text.slen,
              ci.state_text.ptr));
}

/* Callback called by the library when call's media state has changed */
static void on_call_media_state(pjsua_call_id call_id)
{
    pjsua_call_info ci;

    pjsua_call_get_info(call_id, &ci);

    if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
        // When media is active, connect call to sound device.
        pjsua_conf_connect(ci.conf_slot, 0);
        pjsua_conf_connect(0, ci.conf_slot);
    }
}

#endif /* my_app_h */

您不能为回调使用方法,因为方法具有回调所没有的
self
cmd
参数。您是否考虑过使用块?请提供任何帮助:)在使用pjsip时,我完全陷入了这一困境。你找到解决方案了吗?你不能使用方法进行回调,因为方法有
self
cmd
参数,而回调没有这些参数。你考虑过使用块吗?如果有任何帮助,我们将不胜感激:)我在使用pjsip时完全陷入了这个困境。你找到解决办法了吗?