2

Please help with advice. For some reason, Background fetch on a real iOS device does not work. When running through Xcode (Debug -> Sumulate Background Fetch) everything works correctly. But when I install the app on a real device, this function does not work.

I’ll clarify the following:

  1. I do not kill the application, but just minimize it.
  2. The Background App Refresh setting is enabled both in the settings of the app and in the settings of the entire phone.
  3. I waited more than a day - nothing happened.

What seems to me to be the reason:

  1. I run the app on the iPhone through TestFlight.
  2. There is no SIM card installed on the iPhone.
  3. In my app, the standard name of the AppDelegate file was changed to BaseDelegate, and its location was changed from MyApp/MyApp/AppDelegate.swift to MyApp/MyApp/base/BaseDelegate.swift (the app is correctly configured for the new name and location of this file). Can it somehow affect?

What I've done:

  1. I turned on the Background fetch mode in the project settings.
  2. I added the following code to info.plist
<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
</array>
  1. The following code has been added to AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 
    UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum) 
}

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    var message = UserDefaults.standard.string(forKey: "fetch") ?? "nil"
    message = "\(message) | \(Helper.getStringFromDate(Helper.getCurrentDate()))"
    UserDefaults.standard.set(message, forKey: "fetch")

    // Sending local notification ...

    // Receiving data from the server ...

    completionHandler(.newData)
}

To test the app, I send a local notification, and also save the time in UserDefaults.

Vergiliy
  • 1,248
  • 1
  • 13
  • 22
  • A whole bunch of questions: 1. You might double check [`backgroundRefreshStatus`](https://developer.apple.com/documentation/uikit/uiapplication/1622994-backgroundrefreshstatus) to make sure the absence of the SIM hasn’t set this to something other than `.available`. Maybe log this in `didFinishLaunchingWithOptions`, which will make sure your app delegate is getting called and that the status is what you think it is. 2. I assume the device is attached to power and wifi on? – Rob Aug 16 '19 at 19:55
  • 1
    3. I’m assuming that you’re calling that completion handler only after your asynchronous network activity is done? Your code snippet looks like you just call it immediately (and once you’ve done that once without actual performing network request, your app will no longer participate in background fetch). 4. I’m assuming you’ve actually requested permission for local notifications somewhere? – Rob Aug 16 '19 at 19:55
  • @Rob, thank you for your answers. My comments: 1. Thanks for the idea, check out the `UIBackgroundRefreshStatus`. As soon as the result appears - I will write here. 2. Yes, the device is connected to charging and to Wi-Fi. 3. Yes, the completion handler is called after an asynchronous request to the network. 4. Yes, I get permission to send notifications. Notifications are sent correctly. – Vergiliy Aug 17 '19 at 08:17
  • @Rob Checked the theory with `UIBackgroundRefreshStatus`. As a result, I always get `.available`. There are no problems in the code, but on the real device, `Background fetch` still does not work. The second third day has already gone... – Vergiliy Aug 19 '19 at 07:34
  • It seems that the only thing that can cause this is that the application is being tested through `TestFlight`. – Vergiliy Aug 19 '19 at 07:41
  • Have the same issue, but my AppDelegate has original name and location – zslavman Jan 01 '21 at 14:10
  • It helps for me https://developer.apple.com/forums/thread/107784 `UIBackgroundModes fetch remote-notification ` – Taras May 19 '22 at 21:21

0 Answers0