NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Celsius/Fahrenheit per user location.

Forums > SwiftUI

Is there a way to detect the users locale and set the temperature format (Celsius/Fahrenheit) based on that? I couldn't find a solution that suits my needs.

I want the user to be able to set a temperature for a certain task (say for baking cookies) but I'm missing the right path. The way it works in my head is that the user has to choose their format in the apps setting but I'm not sure if I'm thinking in the right direction.

Thanks for your advice!


Whether a user sees Celsius or Fahrenheit is determined in the Language & Region settings of the device. iOS will use that format automatically when you use the built-in formatting APIs. You don't need to* explicitly set that based on the user's locale.

* And shouldn't. Always respect the user's preferences. What if my locale is the USA but I prefer to see temperatures in Celsius?


Thanks for your quick reply, @roosterboy!

I was thinking in the direction of: @Environment(.colorScheme) as an equivalent to let iOS know we are talking about temperature but I'll dig into the measurement formatter based on your reply. Thanks for pointing me towards that direction!


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.