I want to use background mode in my app so in the capabilities I ticked background fetch and notification But The problem is that just in the simulator every things are ok and in the real iPhone the app will freeze in the background
You can't fetch the the latest content from a server in background mode as Apple only allowed to fetch content in specific reasons eg.
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:
Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.
Instead of these you can fetch the content from server in short period of time eg. 1-2 mins depending of cpu usage
func doUpdate() {
DispatchQueue.global(qos: .default).async(execute: {() -> Void in
self.beginBackgroundUpdateTask()
var response: URLResponse? = nil
var error: Error? = nil
let responseData: Data? = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: response)
// Do something with the result
self.endBackgroundUpdateTask()
})
}
func beginBackgroundUpdateTask() {
backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(withExpirationHandler: {() -> Void in
self.endBackgroundUpdateTask()
})
}
func endBackgroundUpdateTask() {
UIApplication.shared.endBackgroundTask(backgroundUpdateTask)
backgroundUpdateTask = UIBackgroundTaskInvalid
}