在我们的应用程序中,我们需要在用户将应用程序推送到后台时取消注册。我们正在使用 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();
}
de-reg方法如下:
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 调用中发回身份验证详细信息,直到我将应用程序带回前台。有谁知道为什么会这样?
谢谢, 黑塔尔