2011-09-22 40 views
0

在我们的应用中,我们需要在将应用推送到背景时取消注册用户。 我们正在使用PJSIP。我applicationDidEnterBackground:使用PJSIP进行SIP注销

- (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"); 

    }); 
    } 

的deregis方法如下:

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

} 

和去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呼叫中发送认证详细信息,直到我将应用程序带回前台。 有谁知道为什么会发生这种情况?

感谢, Hetal

回答

1

你不想结束在你的后台线程后台任务:

例如

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"); 

}); 

您希望在更新注册时结束后台任务。所以你需要钩入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 
    } 
}