iOS 9:beginBackgroundTaskWithExpirationHandler在timout之前被调用 [英] iOS 9 : beginBackgroundTaskWithExpirationHandler is getting called before timout

查看:127
本文介绍了iOS 9:beginBackgroundTaskWithExpirationHandler在timout之前被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行VOIP通话并添加对iOS的支持< 10.
当应用程序处于后台时,对于传入的VOIP呼叫,我正在使用UILocalNotification(在iOS 10中已弃用)。

i'm working on VOIP call and adding support to iOS < 10. For incoming VOIP call when app is in background, i'm using UILocalNotification (deprecated in iOS 10).

拨打电话60秒(或1分钟)我正在使用此代码

To make a call 60 secs (or 1 minute) i'm using this code

    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秒?

to extend background process time to 1 minute and it's working in iOS 10.1.1 (iPhone) but not working in iOS 9.3.5 (iPad). Somehow handler is invoking 30-33 secs?

更新:

我试图评论此代码:

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

我仍然可以做30-33秒,即我不知道为什么这不是工作?

Still i'm able to do 30-33 secs i.e i don't know why this is not working?

推荐答案

我建议您安排几个UILocalNotification,每个都有一个增加的fireDate,然后设置你的UILocalNotification声音持续增加的间隔。例如,假设您的声音可以持续20秒,那么您可以在0,+ 20,+ 40秒后使用fireDate安排3次UILocalNotification。

Instead of using an NSTimer, I will suggest you schedule several UILocalNotification, each has an increasing fireDate, then set your UILocalNotification sound last for the increased interval. For example, say your sound can last for 20 seconds, then you schedule 3 UILocalNotification with fireDate at 0,+20,+40 seconds from now.

当然如果用户接听您的电话,您需要取消剩余的UILocalNotification。

Of course if user answers your call you need to cancel the remaining UILocalNotification.

这篇关于iOS 9:beginBackgroundTaskWithExpirationHandler在timout之前被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆