如何在iOS中运行已经运行的任务使用beginBackgroundTaskWithExpirationHandler [英] How to use beginBackgroundTaskWithExpirationHandler for already running task in iOS
本文介绍了如何在iOS中运行已经运行的任务使用beginBackgroundTaskWithExpirationHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用中,我将图像从iPhone上传到服务器。同时如果用户按下主页按钮,应用程序将关闭。
In my app, I am uploading images to server from iPhone. While sync if user press home button, App will close.
我希望应用程序必须在后台运行,直到同步结束。
I want app must be running in background till sync finish.
我的问题是:如何使用 beginBackgroundTaskWithExpirationHandler
添加当前正在运行的任务?
My question is: How can I add currently running task with "beginBackgroundTaskWithExpirationHandler
"?
请分享你的想法。
推荐答案
我使用下面的代码作为后台任务处理程序:
I used below code for background task handler:
__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background Time:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];
backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
这篇关于如何在iOS中运行已经运行的任务使用beginBackgroundTaskWithExpirationHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文