aws-amplify / docs

AWS Amplify Framework Documentation
https://docs.amplify.aws
Apache License 2.0
478 stars 1.01k forks source link

iOS "Project Setup" documentation should be updated to cover SwiftUI Apps using @main #2075

Closed jtbergman closed 3 years ago

jtbergman commented 4 years ago

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.

lawmicha commented 3 years ago

Hi @jtbergman, thanks for pointing this out, I added something similar over in https://github.com/aws-amplify/docs/pull/2916 addressing the same issue from another developer.