Apple used to have some Sample code for exactly what you're after and it looks like someone has thought to save it even though Apple seems to no longer offer it on their dev site. ;)
Enjoy!
https://github.com/acekiller/iOS-Samples/tree/master/BubbleLevel