簡體   English   中英

后台任務僅在模擬器上有效

[英]Background Task works on Simulator only

背景

我創建了一個示例計時器應用程序,該應用程序從特定值計數到零。 標簽會顯示還剩多少時間。

每次NSTimer (間隔為1s)調用countDownSecs()它都會打印剩余的秒數並執行println("countDownSecs()") 當倒計時達到0秒時,它將發送本地推送通知。
在可正常運行的模擬器運行甚至大約5分鍾后,它將停留在后台並連續打印剩余的秒數和countDownSecs()
另一方面,在實際設備(iPhone 4S,iOS 8.1.3和iOS 8.3)上運行時,進入背景時它會立即停止。 (它以前在帶iOS 8.3的系統上運行。我認為可能是因為iPhone 4S無法處理該問題,但iPhone 4S模擬器運行得很好。)

AppDelegateapplicationDidEnterBackground()我為UILocalNotification提供了以下代碼:

var alarmTime: NSDate = NSDate()
var app: UIApplication = UIApplication.sharedApplication()
var notifyAlarm: UILocalNotification = UILocalNotification()

//counterAll contains the total amount of seconds        
alarmTime.dateByAddingTimeInterval(NSDate.timeIntervalSinceReferenceDate())
notifyAlarm.fireDate = NSDate(timeIntervalSinceNow: counterAll)
notifyAlarm.timeZone = NSTimeZone.defaultTimeZone()
notifyAlarm.repeatInterval = NSCalendarUnit(0)
notifyAlarm.soundName = "Time's Up.m4a"
notifyAlarm.alertBody = "Time's Up! - Your timer has ended."
app.scheduleLocalNotification(notifyAlarm)

我可以更改任何內容來解決此問題嗎?

提前致謝 :)

默認情況下, NSTimer實際設備中不會在后台運行。 適用於模擬器 但是,您可以使用UIApplication的方法beginBackgroundTaskWithExpirationHandler來執行后台任務,但是Apple為后台執行設置了硬限制(只有在我沒有記錯的情況下,才需要180秒)。

建議使用UILocalNotification代替后台任務。

暫無
暫無

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

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