Ipad Orientation Issues.

On a recent i-Pad development project using Phonegap and the Ionic framework we ran into issues with the orientation seemingly being set to portrait only. We had been labouring under the somewhat misplaced apprehension that changes in orientation were just supported with no code being necessary.

Upon looking further into this and seeing what other peoples experiences were there seemed to be a lot of people writing handlers to display when the orientation changed such as this:-

window.addEventListener('orientationchange', doOnOrientationChange,true);

function doOnOrientationChange(e){

  alert('orientation event fire');

 switch(e.orientation){

    case-90:case90:

       alert('landscape');

       break;

    default:

       alert('portrait');

       break;

  }
}

On trying this for ourselves we realised that this ‘orientation change’ event was not being fired. Further googling came across this little gem which actually corrected the issue for us.

function shouldRotateToOrientation(interfaceOrientation){
return true;

}

Seemingly Phone gap requires this function to be defined somewhere within your javascript, we embedded it within the main index.html as an inline script for no other reason than we could. This will probably change at a later date

Advertisements