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:
- I do not kill the application, but just minimize it.
- The
Background App Refresh
setting is enabled both in the settings of the app and in the settings of the entire phone. - I waited more than a day - nothing happened.
What seems to me to be the reason:
- I run the app on the iPhone through
TestFlight
. - There is no SIM card installed on the iPhone.
- In my app, the standard name of the
AppDelegate
file was changed toBaseDelegate
, and its location was changed fromMyApp/MyApp/AppDelegate.swift
toMyApp/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:
- I turned on the
Background fetch
mode in the project settings. - I added the following code to
info.plist
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
</array>
- 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.