Problem
I recently started learning Amplify for use in SwiftUI apps. The documentation shows how to set up a project using the UIApplicationDelegate approach. In iOS 14, there is the App approach using only SwiftUI. It is not clear how to call Amplify.configure() when using this approach.
Solution
The docs could be updated to cover both types of apps. This doesn't require a new set of docs, but just a callout on how to add an app delegate to SwiftUI only apps. For example, this tutorial shows how to add an app delegate. Once that is done nothing needs changed.
Alternatives
N/A
Additional context
The solution I found was to create a file named AppDelegate with
import Foundation
import UIKit
import Amplify
class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
do {
Amplify.Logging.logLevel = .verbose
try Amplify.configure()
} catch {
print("Amplify could not be configured: \(error)")
}
return true
}
}
Then update the App to the following
import SwiftUI
@main
struct AuthenticationApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
I realize that this is a small change, but with SwiftUI Apps being so new, it was not immediately clear that an AppDelegate could be added.
Problem I recently started learning Amplify for use in SwiftUI apps. The documentation shows how to set up a project using the
UIApplicationDelegate
approach. In iOS 14, there is theApp
approach using only SwiftUI. It is not clear how to callAmplify.configure()
when using this approach.Solution The docs could be updated to cover both types of apps. This doesn't require a new set of docs, but just a callout on how to add an app delegate to SwiftUI only apps. For example, this tutorial shows how to add an app delegate. Once that is done nothing needs changed.
Alternatives N/A
Additional context The solution I found was to create a file named AppDelegate with
Then update the
App
to the followingI realize that this is a small change, but with SwiftUI Apps being so new, it was not immediately clear that an
AppDelegate
could be added.