iOS 13 ObservedObject is not updating the UI while StateObject doing same in iOS 14

86 Views Asked by At

"Resolving UI Update Issues with iOS 13 ObservedObject: Exploring Alternatives to StateObject"

"I've explored several alternatives for StateObject, which is only available in iOS 14. Unfortunately, I need a solution compatible with iOS 13. Any guidance or suggestions on addressing this problem would be highly appreciated."

How to achieve same thing in iOS 13 with ObservedObject??

I tried for ObservedObject in iOS 13 but that is not working to update the UI.

Code

import SwiftUI

struct AiicoTermsConditionsView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var viewModel: AiicoTermsConditionsViewModel
    @ObservedObject var webmodel = WebViewModel(url: "")
    
    let backArrowImg = "arrow.left"
    
    var btnBack : some View { Button(action: {
        self.presentationMode.wrappedValue.dismiss()
    }) {
        HStack {
            Image(backArrowImg)
                .aspectRatio(contentMode: .fit)
                .foregroundColor(.colorBlack)
        }
    }
    }
    
    var rightBarButton : some View { Button(action: {
        
    }) {
        HStack {
            viewModel.rightButtonBar()
        }
    }
    }
    
    
    
    var body: some View {
        GeometryReader { geo in
            VStack{
                
                NavigationLink(destination: InsuranceSelectPlanView(viewModel: InsuranceSelectPlanViewModel(rechargeData: viewModel.rechargeData, title: viewModel.title, billerType: viewModel.billerType, isDoctorDay: viewModel.isDoctorDay, selectedPlanData: viewModel.selectedPlanData, selectedHospitalData: viewModel.selectedHospitalData, insuranceType: viewModel.insuranceType, formType: .selfDetailsForm, detailFormModel: viewModel.detailFormModel, KinFormModel: viewModel.KinFormModel, documentFormModel: viewModel.documentFormModel, isFromPreview: false, selectHospitalMode: .purchasePolicy)),  isActive: $viewModel.navigateTONext){
                    EmptyView()
                }
                
                if viewModel.isDoctorDay {
                    Text("docterdey_about_message".localized.toString())
                        .font(.kMontserratMedium14)
                        .padding([.leading,.trailing],20)
                    
                    Spacer()
                }
                else
                {
                    // webview
                    LoadingView(isShowing: $webmodel.isLoading) { WebView(viewModel:  webmodel) }
                }
                
                // Bottom button
                NWRoundedButton(title: kNext,
                                textColor: Color.colorGolden,
                                buttonBackgroundColor: Color.colorBlack,
                                action:
                                    {
                    viewModel.navigateTONext = true
                    
                    
                })
                .frame(height: 70)
            }
            .onAppear(){
                
            }
            .navigationTitle("about_insurance".localized.toString()).font(.kMontserratRegular17)
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: btnBack , trailing: rightBarButton)
        }
    }
}
1

There are 1 best solutions below

0
Ajharudeen khan On

In iOS 13, you can't use the @StateObject property wrapper because it's introduced in iOS 14. Instead, you can use @ObservedObject in combination with a workaround to achieve similar functionality. Here's how you can modify your code to work with iOS 13:

import SwiftUI

struct AiicoTermsConditionsView: View {
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var viewModel: AiicoTermsConditionsViewModel
    @ObservedObject var webmodel: WebViewModel
    
    // Initialize webmodel in init
    init() {
        self.webmodel = WebViewModel(url: "")
    }
    
    let backArrowImg = "arrow.left"
    
    var btnBack: some View {
        Button(action: {
            self.presentationMode.wrappedValue.dismiss()
        }) {
            HStack {
                Image(backArrowImg)
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(.colorBlack)
            }
        }
    }
    
    var rightBarButton: some View {
        Button(action: {
            // Handle right button action here
        }) {
            HStack {
                viewModel.rightButtonBar()
            }
        }
    }
    
    var body: some View {
        GeometryReader { geo in
            VStack {
                NavigationLink(
                    destination: InsuranceSelectPlanView(viewModel: InsuranceSelectPlanViewModel(rechargeData: viewModel.rechargeData, title: viewModel.title, billerType: viewModel.billerType, isDoctorDay: viewModel.isDoctorDay, selectedPlanData: viewModel.selectedPlanData, selectedHospitalData: viewModel.selectedHospitalData, insuranceType: viewModel.insuranceType, formType: .selfDetailsForm, detailFormModel: viewModel.detailFormModel, KinFormModel: viewModel.KinFormModel, documentFormModel: viewModel.documentFormModel, isFromPreview: false, selectHospitalMode: .purchasePolicy)),
                    isActive: $viewModel.navigateTONext
                ) {
                    EmptyView()
                }
                
                if viewModel.isDoctorDay {
                    Text("docterdey_about_message".localized.toString())
                        .font(.kMontserratMedium14)
                        .padding([.leading, .trailing], 20)
                    
                    Spacer()
                } else {
                    // webview
                    LoadingView(isShowing: $webmodel.isLoading) {
                        WebView(viewModel: webmodel)
                    }
                }
                
                // Bottom button
                NWRoundedButton(
                    title: kNext,
                    textColor: Color.colorGolden,
                    buttonBackgroundColor: Color.colorBlack
                ) {
                    viewModel.navigateTONext = true
                }
                .frame(height: 70)
            }
            .onAppear() {
                // Perform any necessary setup here
            }
            .navigationTitle("about_insurance".localized.toString())
            .font(.kMontserratRegular17)
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: btnBack, trailing: rightBarButton)
        }
    }
}