iphone - 使用 PJSIP 取消 SIP 注册

标签 iphone ios ipad sip pjsip

在我们的应用程序中,当用户将应用程序推送到后台时,我们需要取消用户注册。 我们正在使用 PJSIP。我的应用程序DidEnterBackground:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"did enter background");


     __block UIBackgroundTaskIdentifier bgTask;

     bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self deregis];        
        [application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
        bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
        NSLog(@"\n\nRunning in the background!\n\n");

    });
     }

注销方法如下:

- (void)deregis {
    if (!pj_thread_is_registered())
    {
        pj_thread_register("ipjsua", a_thread_desc, &a_thread);
   }    
    dereg();

}

解除注册方法如下:

void dereg()
{
    int i;
    for (i=0; i<(int)pjsua_acc_get_count(); ++i) {
        if (!pjsua_acc_is_valid(i))
             pjsua_buddy_del(i);

        pjsua_acc_set_registration(i, PJ_FALSE);
    }
}

当我们将应用程序推送到后台时,dereg 就会被调用。但是,当服务器发回 401 质询时,堆栈不会发回 SIP 调用中的身份验证详细信息,直到我将应用程序带回到前台。 有谁知道为什么会发生这种情况?

谢谢, 赫塔尔

最佳答案

您不想在后台线程中结束后台任务:

例如

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self deregis];        
    // don't do below...
    // [application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
    // bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
    NSLog(@"\n\nRunning in the background!\n\n");

});

您想要在注册更新时结束后台任务。因此,您需要 Hook pjsua on_reg_state 回调。

例如此示例可能仅假设一个帐户已注销,对于多个帐户,您必须等到所有帐户都已注销

-(void) regStateChanged: (bool)unregistered {
    if (unregistered && bgTask != UIBackgroundTaskInvalid) {
        [application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
        bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
    }
}

关于iphone - 使用 PJSIP 取消 SIP 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7521369/

相关文章:

iphone - 如何在Cocos2D中的多个场景中访问一个ivar?

iphone - 我想列出一个单词列表,但是我不能选择如何存储它。

iphone - Amazon S3 getDate() API 调用?

iphone - 我将如何使用 MapKit 在带有邮政编码/邮政编码的商店上放置图钉

ios - 在 objective-c 中抛出自定义异常

android - FCM 在同时使用 iOS 和 Android 时处理问题

ios - Swift - 从远程 URL 下载视频并将其保存在相册中

iphone - 我们如何让应用程序用户知道连接是通过 SSL 建立的?

javascript - .htaccess 受密码保护的网站在 Ipad 上重新加载每个页面时都需要密码

iphone - Objective-C:在 iOS 中转发消息