Limit battery charging to a certain level?

There's really no option in the iPhone to limit it to charging the battery to 50%, or temporarily disable charging.

However, I suggest that you turn the problem around and look at it differently. Namely just stop supplying power to the phone, when you do not want it to charge. You can leave the cable plugged in and have data transfers continually, but just don't supply the +5V for charging.

This can be done by plugging the iPhone into a hub that supports per-port power control and then plugging the hub into a computer, where you can use uhubctl to turn on/off ports as you like:

https://github.com/mvp/uhubctl