Frank
Frank

Reputation: 11

Running code when app is terminated. Xcode 12

I'm trying to run some code to refresh data and serve a local notification on iOS 14. I'm using Xcode 12 and all the help I find online is based on a structure having an appdelegate.swift swift file with several functions depending on the state of the app.

In Xcode 12 the appdelegate file isn't there anymore. How do I proceed, I don't know where to start. Does anybody know what to do?

Upvotes: 1

Views: 711

Answers (1)

aramusss
aramusss

Reputation: 2401

Your issue is probably that you set up the project to use SwiftUI. You need to create an AppDelegate object that implements UIApplicationDelegate protocol:

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("Your code here")
        return true
    }

    // Implement other methods that you require.

}

On your SwiftUI file, add this line to tell iOS who's gonna be the listener for the AppDelegate's methods:

@main
struct SomeApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

You can then follow by checking Apple's documentation on how to use background app modes.

Upvotes: 1

Related Questions