我正在处理 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 秒,即我不知道为什么这不起作用?