前提・実現したいこと
Xcodeにてカウントダウンタイマーアプリを作成しております。
アプリを起動してホームボタンを押してバックグラウンド動作になった際
シュミレーターではちゃんと動作するのですが
実機でテストすると経過した時間が3分の1程度になってしまいます。
例えば1分のタイマーをセットしスタートさせてすぐにバックグラウンド状態にします。そして30秒経過したら再びアプリを起動させます。
すると
シュミレーターでのテストでは残り時間30秒なのですが
実機でのテストでは残り時間50秒程度になってしまいます。
バックグラウンドでの動作は長時間でなくて最低限数分動けば良いと思っております。
できるのであれば長時間の方がいいですが,,,
ご回答よろしくお願い致しますm(._.)m
発生している問題・エラーメッセージ
なし
該当のソースコード
swift
1import UIKit 2 3@UIApplicationMain 4class AppDelegate: UIResponder, UIApplicationDelegate{ 5 6 var window: UIWindow? 7 var backgroundTaskID : UIBackgroundTaskIdentifier = 0 8 9 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 10 11 } 12 13 func applicationWillResignActive(_ application: UIApplication) { 14 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 15 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 16 17 self.backgroundTaskID = application.beginBackgroundTask(){ 18 [weak self] in 19 application.endBackgroundTask((self?.backgroundTaskID)!) 20 self?.backgroundTaskID = UIBackgroundTaskInvalid 21 } 22 23 } 24 25 func applicationDidEnterBackground(_ application: UIApplication) { 26 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 27 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 28 } 29 30 func applicationWillEnterForeground(_ application: UIApplication) { 31 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 32 33 } 34 35 func applicationDidBecomeActive(_ application: UIApplication) { 36 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 37 application.endBackgroundTask(self.backgroundTaskID) 38 } 39 40 func applicationWillTerminate(_ application: UIApplication) { 41 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 } 43}
試したこと
シミュレーターのerase content and settings
シュミレーター再起動
Xcodeの再起動
実機からアプリを消し再インストール
実機の設定、アプリのバックグラウンド更新をオンオフ
補足情報(FW/ツールのバージョンなど)
Swift 4.1
実機
ios 11.2 iPhone6s
iOS 10.3 iPhone5
シミュレーター
ios 11.2 iPhone6s
iOS 11.3 iPhone6s

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/30 11:54