This is a tricky one, because although these two expressions sound roughly the same to a foreign ear, Norwegians have very distinct contexts in which they use these expressions. In Norwegian classes nobody teaches you how to use these expressions. Instead foreigners learn expressions like Ugler i mosen and Svin på skogen, see this article for more Norwegian expressions.
But reality is that Norwegians do say both of these expressions a lot. Although they seem similar, using them to say the same thing would be a huge cultural mistake and would raise eyebrows from Norwegians at best. They would be shocked at your lack of sensitivity at worst. Keep in mind though that neither of these are swear words in Norwegian. To read more about swearing in Norwegian check this blogpost I wrote What the Faen? How to Swear in Norwegian.
Americans with Norwegian heritage are probably those who know how to differentiate them the most among “foreigners” since they have heard them from their grandparents and parents since childhood.
When to use Uff da?
Uff da is being used when something bad happened, but not too serious. For example if a child falls and starts crying because he hurt himself, a Norwegian parent would say “Uff da, har du vondt?” (Uff da, are you hurt?). It is a kind way of saying that you care, yet not involving yourself too much. It also shows that you acknowledge the pain or discomfort the person had to go through.
YourWay2Norway explained in a video called The ultimate guide to Uff da, that Uff da is used by Norwegians to show empathy, but for some serious things it cannot be used. It can actually seem offensive to Norwegians to use Uff da in the wrong context. For example if a person tells you they have cancer, Uff da is definitely NOT the right answer. It is way too trivial to use in such serious circumstances.
Anything which is expected but negative can get an Uff da response. A dog died, an elderly person had to go to a retirement home, etc.
Uff da can also be used with humour by Norwegians. If you tell a Norwegian a story of something quite amusing but still a bit negative, where the person might think you are exaggerating, they will answer Uff da with a hidden laugh.
When to use Uffa meg instead
Uff da should not be used in circumstances where the story is very serious. Then you need to show that you REALLY care.
If you tell a Norwegian than you father or mother is very sick and will surely pass away soon, or that your child is being bullied at school, the answer will probably be Uffa meg. Followed by the Norwegian sharing their own personal story of something similar happening to them. Opening up and sharing personal details means that they REALLY care and empathise with whatever you are going through.
If a Norwegian is ready to offer help (driving you to the hospital for ex.) it will surely be after he/she said “uffa meg” to your story, not “uff da”.
The real test of the difference between Uff da and Uffa meg is in real life conversation. There is nothing like making that mistake and having a Norwegian stare at you like a person who has just been betrayed “I just told you that my grand mother, the only person who raised me, just got hit by a car and is in a coma in a hospital, and all you have to answer is Uff da?”. Well, then all you can say for your defense is that Norwegian is your third language, you don’t know what to say. “What would you have expected me to say?” is something you can ask.
Other articles about Norwegian language to read here if you are interested: How to differentiate the Norwegian Dialects? – Is there a Typical Norwegian Humour? – Is Norwegian a Language of Love? – Is Norwegian Hard to Learn?