How to create callBack in iOS that should be called by the library

泪湿孤枕 提交于 2019-12-11 02:22:59

问题


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.

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.

Here is the code snippet:

/* 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);
}

回答1:


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 

}

Now you can observe this notification in the AppDelegate

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

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];
        });

}


来源:https://stackoverflow.com/questions/41537727/how-to-create-callback-in-ios-that-should-be-called-by-the-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!