2

I need help migrating my code for iOS 16 support:

//
//  RegistrationView.swift
//  Recosia
//
//  Created by Alex Slater on 16/11/2022.
//

import SwiftUI

struct RegistrationView: View {
    @State private var email = ""
    @State private var username = ""
    @State private var fullname = ""
    @State private var password = ""
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var viewModel: AuthViewModel
    @State var root = [Root]()
    
    var body: some View {
        
        VStack {
            NavigationStack(root: $root) {
                //content
                .onChange(of: viewModel.didAuthenticateUser) { newValue in
                    guard newValue else {return}
                    root.append(.profile)
                }.navigationDestination(for: Root.self) { navigation in
                    switch navigation {
                        case .profile:
                            ProfilePhotoSelectorView()
                    }
                }
            }
        }
//            NavigationLink(destination: ProfilePhotoSelectorView(),
//                           isActive: $viewModel.didAuthenticateUser,
//                           label: { })
            
            AuthHeaderView(title1: "Sign up", title2: "to Recosia")
            
            VStack(spacing: 40) {
                    CustomInputField(imageName: "at.circle.fill", placeholderText: "Username", text: $username)
                    
                    CustomInputField(imageName: "person", placeholderText: "Full Name", text: $fullname)
                    CustomInputField(imageName: "envelope", placeholderText: "Email", text: $email)
                    
                CustomInputField(imageName: "lock",
                                 placeholderText: "Password",
                                 isSecureField: true,
                                 text: $password)
                
            }
            .padding(32)
            
            Button {
                viewModel.register(withEmail: email,
                                   password: password,
                                   fullname: fullname,
                                   username: username)
            } label: {
                Text("Create Account")
                    .font(.headline)
                    .foregroundColor(.white)
                    .frame(width: 340, height: 50)
                    .background(Color(.systemPink))
                    .clipShape(Capsule())
                    .padding()
                    
            }
            .shadow(color: .gray.opacity(0.5), radius: 10, x: 0, y: 0)
            
            Spacer()
            
            Button {
                presentationMode.wrappedValue.dismiss()
            } label: {
                HStack {
                    Text("Already have an account?")
                        .font(.caption)
                        .foregroundColor(Color(.black))
                    
                    Text("Sign In")
                        .font(.footnote)
                        .fontWeight(.semibold)
                        .foregroundColor(Color(.systemPink))
                }
                .padding(.bottom, 32)
            }
            }
            .ignoresSafeArea()
        }
    }

struct RegistrationView_Previews: PreviewProvider {
    static var previews: some View {
        RegistrationView()
    }
}

enum Root {
    case profile
}

with the error:

init(destination:isActive:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:) inside a NavigationStack or NavigationSplitView

I have tried re-arranging my code but I'm getting confused, can someone help!

Much appreciated :)

I tried using a NavigationStack with a .navigationDestination, but it says that it expects other arguments but the code presented above is all I have.

robowarrior
  • 123
  • 6

1 Answers1

1

The new NavigationLink no longer requires a destination View. However, it needs some kind of an identifier, so navigationDestination expects the type of the identifier used in order to identify what View should be presented.

In your case, you don't need a NavigationLink anymore since you are navigating based on viewModel.didAuthenticateUser. You can listen to didAuthenticateUser and directly append the identifier to an array that you provide the NavigationStack with. Check init(path:root:):

struct RegistrationView: View {
    @State private var email = ""
    @State private var username = ""
    @State private var fullname = ""
    @State private var password = ""
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var viewModel: AuthViewModel
    @State var root = [Root]()
    var body: some View {
        NavigationStack(path: $root) {
            VStack {
                AuthHeaderView(title1: "Sign up", title2: "to Recosia")
                
                VStack(spacing: 40) {
                    CustomInputField(imageName: "at.circle.fill", placeholderText: "Username", text: $username)
                    
                    CustomInputField(imageName: "person", placeholderText: "Full Name", text: $fullname)
                    CustomInputField(imageName: "envelope", placeholderText: "Email", text: $email)
                    
                    CustomInputField(imageName: "lock",
                                     placeholderText: "Password",
                                     isSecureField: true,
                                     text: $password)
                    
                }
                .padding(32)
                
                Button {
                    viewModel.register(withEmail: email,
                                       password: password,
                                       fullname: fullname,
                                       username: username)
                } label: {
                    Text("Create Account")
                        .font(.headline)
                        .foregroundColor(.white)
                        .frame(width: 340, height: 50)
                        .background(Color(.systemPink))
                        .clipShape(Capsule())
                        .padding()
                    
                }
                .shadow(color: .gray.opacity(0.5), radius: 10, x: 0, y: 0)
                
                Spacer()
                
                Button {
                    presentationMode.wrappedValue.dismiss()
                } label: {
                    HStack {
                        Text("Already have an account?")
                            .font(.caption)
                            .foregroundColor(Color(.black))
                        
                        Text("Sign In")
                            .font(.footnote)
                            .fontWeight(.semibold)
                            .foregroundColor(Color(.systemPink))
                    }
                    .padding(.bottom, 32)
                }
            }
            .ignoresSafeArea()
            .onChange(of: viewModel.didAuthenticateUser) { newValue in
                guard newValue else {return}
                root.append(.profile)
            }.navigationDestination(for: Root.self) { navigation in
                switch navigation {
                    case .profile:
                        ProfilePhotoSelectorView()
                }
            }
        }
    }
    enum Root {
        case profile
    }
}
Timmy
  • 4,098
  • 2
  • 14
  • 34