It's certainly possible to hit someone while they work on a takedown. It sounds like you're asking specifically about countering wrestling shots, where the grappler attacks the hips and legs, rather than clinch takedowns generally, so I'll focus on that. (Nevertheless, remember that upper-body clinch takedowns are common and effective as well.) Punishing wrestling shots with knees is a good but not foolproof strategy that requires practice both on the counter and on what to do when it fails. The success rate is below 50%.
Striking an opponent as they shoot is absolutely allowed in MMA, except for a narrow rule against targeting the two-inch strip at the back of the head and the spine. All other targets are legal, and striking an opponent as they try to take you down is definitely allowed. Sometimes it works spectacularly, such as when Jorge Masvidal made Ben Askren stiff as a board with a flying knee, or when Travis Browne knocked out several opponents with elbow strikes while they worked to secure a double-leg grip against the cage.
There's another rule to be aware of, which both helps and hinders grapplers. In most modern MMA rulesets, it's illegal to knee or kick the head of an opponent on the ground. (This wasn't true in the now-defunct Pride organization, so watch those old Japanese fights to see how MMA worked without this ban.) This means that if a fighter shoots for your legs and finds themself in a turtle because they can't finish the takedown, they don't have to worry about the obvious counter from their sprawled opponent: knees to the head. However, this rule also punishes good wrestlers and grapplers, because if they are able to finish their takedown, they can't take advantage of one of the most powerful ground attacks.
Sometimes—usually—it doesn't work. One of the best examples of what a good-but-not-good-enough knee-strike counter looks like comes from when Conor McGregor tried to catch Khabib Nurmagomedov as the Dagestani wrestler shot in for a single leg takedown. Another example is when Rafael dos Anjos expended a lot of energy to miss a flying knee on the clinching Colby Covington. And don't forget that Masvidal was far from the first to try to catch Askren on the way in — and that knee even connects, flush, hard! It's just not enough. There are many, many more examples where one fighter planned on such a knee counter and couldn't get it off, because the footwork and timing is actually quite difficult. And when it fails, then you're in the wrestler's world. This is why MMA fighters train grappling even if they would prefer to avoid it: you can't always dictate where the fight happens, so you have to prepare. You have to be familiar with the various takedowns, counters, counters to the counters, techniques for standing up once the takedown works, and techniques for defending yourself when the stand-ups don't work.
There are no techniques that render grappling unusable in real world scenarios, because grappling is an ever-present part of fighting. Clinches and wrestling occur naturally in any fight, which is why all striking sports have extensive rules dealing with it as an inevitability. If grappling doesn't work for you in sparring it's probably because you're not yet very good at takedowns.