如何在iOS中创建应由库调用的callBack [英] How to create callBack in iOS that should be called by the library

查看:99
本文介绍了如何在iOS中创建应由库调用的callBack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用PJSIP Library的VOIP_App,该PJSIP Library是用C-Language编写的,该库中编写的大多数方法都会根据情况自动调用.

I am developing a VOIP_App that uses PJSIP Library which is written in C-Language, most of the methods written in that library are called automatically according to the situation.

有一个名为on_incoming_call的方法会自动调用,并且用户会收到呼叫,我想添加一些用户交互来接收呼叫,我需要创建一些应在此方法中调用的callBack,并且方法定义应用Objective-C编写.

There is method named on_incoming_call called automatically and call is received by user, I want to add some user interactions for receiving call, I need to create some callBack, that should be called in this method, and the method definition should be written in Objective-C.

这是代码段:

/* Callback called by the library upon receiving incoming call */
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, "....\n\n\n Incoming call from %.*s!!  \n\n\n",
          (int)ci.remote_info.slen,
          ci.remote_info.ptr));

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

推荐答案

您可以通过在PJSIP库中的回调中发布通知来实现您的目标

You can achieve your objective by posting a notification from the callback in the PJSIP library

static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
                     pjsip_rx_data *rdata){
   pjsua_call_info ci;
   NSString *callerNumber = [NSString stringWithUTF8String:ci.remote_info.ptr];

   [[NSNotificationCenter defaultCenter] postNotificationName:
     @"IncomingCall" object:callerNumber];

pjsua_call_answer(call_id, 200, NULL, NULL); //Comment out this line and instead call this in your UIViewController to accept the call on click of some button or whatever UI action you desire 

}

现在您可以在AppDelegate中查看此通知

Now you can observe this notification in the AppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(launchIncomingVC:) name:@"IncomingCall" object:nil];
}

然后该函数显示您的UIViewController接受或拒绝来电

And then this function to show your UIViewController for accepting or rejecting the incoming call

-(void)launchIncomingVC:(NSNotification *) notif
{
    NSLog(@"LAUNCH INCOMING CALL VC");

    YourViewController *rvc = [[YourViewController alloc] init];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryBoardName" bundle:nil];
    rvc = [storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];

    rvc.paramCallerNumber = noti.object; //pass the caller number to show it on the incoming view

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.window makeKeyAndVisible];

            UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
            while (topRootViewController.presentedViewController)
            {
                topRootViewController = topRootViewController.presentedViewController;
            }

            [topRootViewController presentViewController:incomingVC animated:YES completion:nil];
        });

}

这篇关于如何在iOS中创建应由库调用的callBack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆