posix4e / puppet

MIT License
8 stars 8 forks source link

Setup the main app to configure settings, or hit web client frontend, provide a health check #2

Open posix4e opened 1 year ago

posix4e commented 1 year ago

From chatgpt3

If you want to provide a configuration interface in your main app UI as well, you can create a settings or configuration page in your iOS app using SwiftUI, UIKit, or any other UI framework that you are using.

Here is an example of how you could implement this in SwiftUI:

import SwiftUI

struct ConfigView: View {
    @State private var baseUrl: String = ""
    @State private var uid: String = ""
    @State private var machineId: String = ""

    var body: some View {
        Form {
            Section(header: Text("Configuration")) {
                TextField("Base URL", text: $baseUrl)
                TextField("UID", text: $uid)
                TextField("Machine ID", text: $machineId)
            }
            Button(action: {
                // save configuration to UserDefaults
                UserDefaults.standard.setValue(self.baseUrl, forKey: "base_url")
                UserDefaults.standard.setValue(self.uid, forKey: "uid")
                UserDefaults.standard.setValue(self.machineId, forKey: "machineid")
            }) {
                Text("Save Configuration")
            }
        }
    }
}

This SwiftUI view will show three text fields for entering the BASE_URL, UID, and machineID, as well as a save button that saves these values to UserDefaults.

To display this view, you could replace your existing ViewController with a UIHostingController that hosts this ConfigView:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }

        let contentView = ConfigView()

        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
    }

}

Please note that since you're using an extension, there's currently no way to directly share data between your extension and your app via UserDefaults in iOS. There are some workarounds to do this such as using App Groups, but it's not directly supported.

If your settings in your main app need to be the same with your extension, you will need to create an App Group to share data between your main app and your extension.

Steps to create an App Group:

  1. Click on your project, then under TARGETS, click on your main app.
  2. Go to the Capabilities tab, turn on App Groups, and create a new App Group. It will usually be something like group.com.yourcompany.yourapp.
  3. Do the same thing for your extension.

Then, replace UserDefaults.standard with UserDefaults(suiteName: "group.com.yourcompany.yourapp") in both your extension and your app to use shared UserDefaults.