Is it possible to avoid transition to landscape view in Safari for iOS when the device is rotated?
iOS Safari has the \"orentationchange\" event, I tried to intercep
There is no way to force a particular orientation in Mobile Safari; it'll always autorotate when the user rotates their device.
Perhaps you can display something for unsupported orientations informing the user that the orientations aren't supported, and that they need to rotate the device back in order to use your web app.
The following solution seems working for me on iPhone4, 5, 6, and 6+ as of June 2016.
<script>
/**
* we are locking mobile devices orientation to portrait mode only
*/
var devWidth, devHeight;
window.addEventListener('load', function() {
devWidth = screen.width;
devHeight = screen.height;
});
window.addEventListener('orientationchange', function () {
if (devWidth < 768 && (window.orientation === 90 || window.orientation == -90)) {
document.body.style.width = devWidth + 'px';
document.body.style.height = devHeight + 'px';
document.body.style.transform = 'rotate(90deg)';
document.body.style.transformOrigin = ''+(devHeight/2)+'px '+(devHeight/2)+'px';
} else {
document.body.removeAttribute('style');
}
}, true);
</script>
If it is possible (which I don't believe it is), then I'd strongly advise against it. Users hate being forced into viewing pages in a particular way. What if they're using their iphone in a dock and can't stand it in landscape mode without undocking it, or what if they prefer the landscape version of the keyboard because the keys are bigger?
If your design requires a particular orientation then you might want to rethink your design.