2011-09-22 88 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 
    } 
}