title: SIP deregistration using PJSIP
body: <p>In our app, we need to de-reg a user when he pushes the app to the background.
  We are using PJSIP. My applicationDidEnterBackground:</p>
  
  <pre><code>- (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");
  
      });
       }
  </code></pre>
  
  <p>The deregis method is as below:</p>
  
  <pre><code>- (void)deregis {
      if (!pj_thread_is_registered())
      {
          pj_thread_register("ipjsua", a_thread_desc, &amp;a_thread);
     }    
      dereg();
  
  }
  </code></pre>
  
  <p>And the de-reg method is as below:</p>
  
  <pre><code>void dereg()
  {
      int i;
      for (i=0; i&lt;(int)pjsua_acc_get_count(); ++i) {
          if (!pjsua_acc_is_valid(i))
               pjsua_buddy_del(i);
  
          pjsua_acc_set_registration(i, PJ_FALSE);
      }
  }
  </code></pre>
  
  <p>When we push the app to the background, the dereg gets called. But when the server sends back a 401 challenge, the stack isn't sending back the auth details in SIP call until I bring the application back to foreground. 
  Does anyone have any idea why this is happening?</p>
  
  <p>Thanks,
  Hetal</p>