Improve device orientation detection and fix a refresh bug with iOS