iOS background Fetch is not working in actual devices ( iPad & iPhone)

Viewed 456

I have created an iOS native app using Swift 5.0, which internally fetch data from the server using background fetch feature of iOS if the App is running in background mode. Background fetching is working properly in Simulator (Simulating background fetch in XCode). But background fetches not at all working in the real device (iPad Air & iOS 12.2).

I have done the following steps

  1. Enabled Background fetch of 'Background modes' under "Capabilities" on the target in Xcode. enter image description here

2.Configured minimum background fecth interval and 'performFetchWithCompletionHandler' method in AppDelegate.swift class

 import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

   var window: UIWindow?

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
       UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
       return true
   }

   func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
       if let url = URL(string: "http://192.168.225.24:80/product") {
           URLSession.shared.dataTask(with: url, completionHandler: { (data, respone, error) in
               guard let `data` = data else { completionHandler(.failed); return }
               let result = String(data: data, encoding: .utf8)
               print("performFetchWithCompletionHandler result: \(String(describing: result))")
               completionHandler(.newData)
           }).resume()
       }
   }
}

I checked in Simulator by XCode's Simulate Background Fetch then HTTP request is reaching at the server. Also, I verified that the real device has Background Refresh is enabled in App's settings.

But on real devices, the Background Fetch is not working.
What would be the reason? Please help me to resolve.

0 Answers
Related