0

私たちのアプリでは、ユーザーがアプリをバックグラウンドにプッシュしたときにユーザーの登録を解除する必要があります。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");

    });
     }

deregis メソッドは次のとおりです。

- (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 呼び出しで認証の詳細を送り返しません。なぜこれが起こっているのか誰にも分かりますか?

ありがとう、ヘタル

4

1 に答える 1

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 コールバックにフックする必要があります。

たとえば、この例では 1 つの登録解除のみを想定している可能性があります。複数のアカウントの場合、すべての登録が解除されるまで待つ必要があります。

-(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
    }
}
于 2011-09-26T00:27:33.180 に答える