UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Share data accross screens

Forums > SwiftUI

In my app, I want to store the id of the user so that I can use it on several screens. On one of the screens, I will want to read/write the id, but on the others I'll just want to read the value.

I've got something working using UserDefaults to save the user and read it back in the other screens, but changing it on the edit screen does not reflect the change on the other screens (unless I stop and reload the app).

Anyone got example code of this type of thing?

2      

I think what you're looking for is an environment object. Have a look here, it's described quite detailled: https://www.hackingwithswift.com/books/ios-swiftui/reading-custom-values-from-the-environment-with-environmentobject

Hope that helps Greetings Lenny

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.