SWIFTUI I have a windows depreciation error for Xcode

40 Views Asked by At
import SwiftUI
import UIKit
struct ContentView: View {
    @State private var companyName: String = ""
    @State private var address: String = ""
    @State private var city: String = ""
    @State private var state: String = ""
    @State private var zipCode: String = ""
    @State private var phone: String = ""
    @State private var resultMessage: String = ""
    @State private var email: String = ""
    @State private var mobilePhone: String = ""
    @State private var mobileCarrier: String = ""
    @State private var username: String = ""
    @State private var password: String = ""
    @State private var reEnteredPassword: String = ""
    var body: some View {
        VStack {
            Group {
                Text("Registration Form")
                    .padding()
                    .font(.largeTitle)
                TextField( "Enter Company Name", text: $companyName)
                    .textFieldStyle(.roundedBorder)
                TextField ("Enter Address", text: $address)
                    .textFieldStyle(.roundedBorder)
                TextField("Enter City", text: $city)
                    .textFieldStyle(.roundedBorder)
                TextField("Enter State", text: $state)
                    .textFieldStyle(.roundedBorder)
                TextField("Enter Zip Code", text: $zipCode)
                    .textFieldStyle(.roundedBorder)
            }
                Group {
                TextField("Enter Phone Number", text: $phone)
                    .textFieldStyle(.roundedBorder)
                TextField("Enter Email", text: $email)
                    .textFieldStyle(.roundedBorder)
                TextField("Enter Mobile Phone", text: $mobilePhone)
                    .textFieldStyle(.roundedBorder)
                TextField("Enter Mobile Carrier", text: $mobileCarrier)
                    .textFieldStyle(.roundedBorder)
                TextField("Enter Username", text: $username)
                    .textFieldStyle(.roundedBorder)
                TextField("Enter Password", text: $password)
                    .textFieldStyle(.roundedBorder)
                TextField("Re-Enter Password", text: $reEnteredPassword)
                    .textFieldStyle(.roundedBorder)
            }
            Button(action : {
                Task {
                   await validateDataForm()
                }
            }) {
                Text("SUBMIT")
                    .font(.title3)
                    .padding()
                
            }
            Text("\(resultMessage)")
                .bold()
                .padding()
        }
    }
    func validateDataForm() {
        // Validation for Company Name
        if companyName == "" {
            presentAlert(withTitle: "Validation Error", message: "Company Name field is blank!")
            return
        }
        if address == "" {
            presentAlert(withTitle: "Validation Error", message: "Company Name field is blank!")
            return
        }
        resultMessage = "ALL DATA IS VALID"
    }
        private func presentAlert(withTitle title: String, message: String) {
            let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            if let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) {
                window.rootViewController?.present(alertController, animated: true, completion: nil)
            }
        } // end of the function presentAlert
        
    } // end struct
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }



This is the structure of my project that I have to do. All I have to do after I finish the structure is add validation using if else statements.Those will all go in the validate data function. When I try to run this structure code, I keep getting this error on Xcode. Error: 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead. I am running Xcode on a virtual machine. I downloaded Xcode 14.0 on MacOs 13. Is this a possible reason why? I am a beginner on Xcode by the way.

When recieiving help for this problem: I posted this code and got the following advice: I was missing an extra paranthesis for a part of the code and i needed to divide the text boxes within 2 groups. I fixed these but I'm still having the same issue. I am pretty sure that this issue may because of my IOS that I downloaded, but I'm unsure and can't find any supported discussions on this website and the apple help website. EDIT: my question got closed for being already answered. I don't believe this to be the case. I said I'm not using IOS 15. The code is much more complicated than mine so I'm wondering the following:

  • Which part of my current function for presentAlert can change?
  • Do I still change the function to include the public extension of UIApplication?
  • Or am I just overthinking this entire thing and I should replace the line " if let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow })" with "UIApplication.shared.currentUIWindow()?.rootViewController", what the solution linked to my question had said.
0

There are 0 best solutions below