iOS 9 : beginBackgroundTaskWithExpirationHandler is getting called before timout

烈酒焚心 提交于 2019-11-29 16:24:54

I Would Suggest you to Put Code in this Structure and Use this Code

-(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
    });
});
}
ingh.am

As you are building a VOIP application, in info.plist you should be able to set UIBackgroundModes to voip and UIApplicationExitsOnSuspend to NO to allow background tasks.

Check out this apple help page on "Declaring Your App’s Supported Background Tasks" for more information.

When I last built an application that ran in the background (albeit it was for background location services, not for a voip app), I had to put a disclaimer on the app store listing about the app using excessive battery life. I'm not sure if this is relevant anymore though.

Also see this answer on SO.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!