gpt4 book ai didi

ios - 如何在 iOS 中创建应由库调用的回调

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:34 24 4
gpt4 key购买 nike

我正在开发一个使用 PJSIP 库 的 VOIP_App,它是用 C 语言 编写的,该库中编写的大部分方法都是根据情况自动调用的。

有一个名为 on_incoming_call 的方法会自动调用并且用户会收到电话,我想添加一些用户交互来接听电话,我需要创建一些回调,应该在这个方法中调用,并且方法定义应该用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 库中的回调发布通知来实现您的目标

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 中观察到此通知

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

然后这个函数显示你的 UIViewController 接受或拒绝来电

-(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 - 如何在 iOS 中创建应由库调用的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41537727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com