Search code examples
iosswift3ios10background-fetch

Swift3 - Background Fetch Crash on Debug


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    print(UIApplicationBackgroundFetchIntervalMinimum)
    UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
    return true
}

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    completionHandler(.newData)
}

This is my code and when I Debug in XCode > Simulate background fetch my app crash without reason. I activated in Capabilities>Background modes>Background fetch... any ideas?

It crash only on iPhone in simulator I don't have problems.


Solution

  • As per your above description, I tried it in my project but it works fine and performFetchWithCompletionHandler is working in background also.

    First I try without adding this property, but performFetchWithCompletionHandler not called. So,Try to add this in info.plist file

     <key>UIBackgroundModes</key>
        <array>
            <string>fetch</string>
        </array>
    

    It will solve my problem