簡體   English   中英

iOS 9:beginBackgroundTaskWithExpirationHandler在timout之前被調用

[英]iOS 9 : beginBackgroundTaskWithExpirationHandler is getting called before timout

我正在進行VOIP調用並添加對iOS <10的支持。對於應用程序處於后台時的傳入VOIP調用,我正在使用UILocalNotification(在iOS 10中已棄用)。

要撥打電話60秒(或1分鍾),我正在使用此代碼

    count = 0;
                    apnTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
                                                                target:self
                                                              selector:@selector(showIncomingCall:)
                                                              userInfo:userInfo
                                                               repeats:YES];
    self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
                        NSLog(@"ALVOIP : BACKGROUND_HANDLER_NO_MORE_TASK_RUNNING.");
                        [application endBackgroundTask:self.backgroundTask]; 
                        self.backgroundTask = UIBackgroundTaskInvalid;
                    }];


    -(void)showIncomingCall:(NSTimer *)timer
{
    if (count < 60)
    {

            UIApplication *application = [UIApplication sharedApplication];
            [application presentLocalNotificationNow:localNotification];
            NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);

        count = count + 3;
        return;
    }

    // TIMEOUT : STOP TIMER AND LOCAL NOTIFICATION BACKGROUND TASK
    [self invalidateCallNotifying];
}

-(void)invalidateCallNotifying
{
    [apnTimer invalidate];
    if (self.backgroundTask != UIBackgroundTaskInvalid)
    {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }
}

將后台處理時間延長到1分鍾,它在iOS 10.1.1(iPhone)中運行,但在iOS 9.3.5(iPad)中無效。 不知何故,處理程序調用30-33秒?

更新:

我試着評論這段代碼:

self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
                        NSLog(@"ALVOIP : BACKGROUND_HANDLER_NO_MORE_TASK_RUNNING.");
                        [application endBackgroundTask:self.backgroundTask]; 
                        self.backgroundTask = UIBackgroundTaskInvalid;
                    }];

我仍然可以做30-33秒,即我不知道為什么這不起作用?

我建議你把代碼放在這個結構中並使用這個代碼

-(void)yourMethod
{
 __block UIBackgroundTaskIdentifier background_task;
background_task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void){
    [[UIApplication sharedApplication] endBackgroundTask: background_task];
    background_task = UIBackgroundTaskInvalid;
}];

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

//Some Methods Calls

    dispatch_async(dispatch_get_main_queue(), ^{
        //View Controller Change
    });
});
}

在構建VOIP應用程序時,在info.plist中,您應該能夠將UIBackgroundModes設置為voip ,將UIApplicationExitsOnSuspendNO以允許后台任務。

有關詳細信息,請查看“聲明應用程序支持的后台任務”中的此Apple幫助頁面

當我上次構建一個在后台運行的應用程序時(盡管它是用於后台位置服務,而不是用於voip應用程序),我不得不在應用程序商店列表中使用過長的電池壽命放置關於應用程序的免責聲明。 我不確定這是否相關。

在SO上也看到這個答案

我建議你安排幾個UILocalNotification,每個都有一個增加的fireDate,然后設置你的UILocalNotification聲音最后增加的間隔,而不是使用NSTimer。 例如,假設您的聲音可以持續20秒,那么您可以在0,+ 20,+ 40秒后使用fireDate安排3次UILocalNotification。

當然,如果用戶接聽您的電話,您需要取消剩余的UILocalNotification。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM