这在前台工作正常,但当应用程序进入后台时,它会在30分钟到一小时后被 *** 作系统杀死.我希望应用程序至少在8小时内获得更新,即使在后台也是如此.
此外,该应用程序每小时使用约10%的电池.这与应用程序在后台被杀是否相关?如果是这样,那么我该如何解决电池问题呢?否则,谁能告诉我这是什么问题?
以下是设备的崩溃日志:
Exception Type: 00000020Exception Codes: 0x000000008badf00dException Note: SIMulATED (this is NOT a crash)Highlighted by Thread: 2Application Specific information:<BKNewProcess: 0x17e74840; com.app.app; pID: 560; hostpID: -1> has active assertions beyond permitted time: {(<BKProcessAssertion: 0x17d78740> ID: 560-C9E81E97-90D9-4F95-871E-3DC53372F302 name: Called by UIKit,from <redacted> process: <BKNewProcess: 0x17e74840; com.app.example; pID: 560; hostpID: -1> permittedBackgroundDuration: 180.000000 reason: finishTask owner pID:560 preventSuspend preventIDleSleep preventSuspendOnSleep,<BKProcessAssertion: 0x17e6a870> ID: 560-BD7B29FC-DABC-42FF-AF17-B277BDB1C59D name: Called by UIKit,from <redacted> process: <BKNewProcess: 0x17e74840; com.app.example; pID: 560; hostpID: -1> permittedBackgroundDuration: 180.000000 reason: finishTask owner pID:560 preventSuspend preventIDleSleep preventSuspendOnSleep )}
对于后台任务,我使用以下函数:
func backgroundTask(){ var application=UIApplication.sharedApplication() var background_task: uibackgroundtaskIDentifIEr? background_task = application.beginBackgroundTaskWithExpirationHandler({() -> VoID in application.endBackgroundTask(background_task!) background_task = uibackgroundtaskInvalID }) dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,0),{() -> VoID in //run the app without startUpdatingLocation. backgroundTimeRemaining decremented from 600.00 self.locationManager.startUpdatingLocation() while (true) { //backgroundTimeRemaining time does not go down. print("Background time Remaining: \(UIApplication.sharedApplication().backgroundTimeRemaining)") NSThread.sleepForTimeInterval(1) break //wait for 1 sec } application.endBackgroundTask(background_task!) background_task = uibackgroundtaskInvalID })}解决方法 当您的应用进入后台状态时,切换到重要的位置更新,您的应用将持续接收位置更新.您可以在CLLocationmanger的对象上调用startMonitoringSignificantLocationChanges.而且我认为你也不需要建立后台任务.
检查Apple Documentation,它说,
If you start this service and your app is subsequently terminated,the system automatically @R_329_4404@es the app into the background if a new event arrives. In such a case,the options dictionary passed to the application:willFinishLaunchingWithOptions: and application:dIDFinishLaunchingWithOptions: methods of your app delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your app was launched because of a location event. Upon @R_329_4404@,you must still configure a location manager object and call this method to continue receiving location events. When you restart location services,the current event is delivered to your delegate immediately. In addition,the location property of your location manager object is populated with the most recent location object even before you start location services
所以,它会解决你的问题,我认为它也将解决电池问题.
第二件事(对于电池消耗),当想要在后台长时间更新位置时,不应将DesiredAccuracy设置为kCLLocationAccuracyBest.您可以将kCLLocationAccuracyThreeKilometers设置为DesiredAccuracy,并且可以在后台输入时将setdistanceFilter设置为非常大的数字,如99999.
您可以参考this so post和this so post.
希望这会有所帮助:)
总结以上是内存溢出为你收集整理的iOS为什么系统在后台使用位置杀死应用程序全部内容,希望文章能够帮你解决iOS为什么系统在后台使用位置杀死应用程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)