iPhone-App with PhoneGap Orientationchange?

267 views
Skip to first unread message

TK

unread,
Jun 7, 2011, 2:08:00 PM6/7/11
to phonegap
Hey,

ive developed my first iphone and android app today.
the problem is that the iphone app doesnt seem to check the
orentationchage-event.
ive tested different approaches:

first:
#$(window).resize(function() {}
works with android native app
doesnt work with native iphone app

second:
#window.addEventListener("orientationchange", onWindowOC, false);
#document.addEventListener("orientationchange", onDocumentOC, false);
works with android native app
doesnt work with native iphone app

third:
#navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess,
accelerometerError);
works with android native app
doesnt work with native iphone app


maybe there is a solution for that problem???
my complete app is working so fine on android, even on iphone it is
working...only the orientation-event wont work.

hope somebody can help me.

best regards

ScottP

unread,
Jun 7, 2011, 2:29:06 PM6/7/11
to phonegap
This is how I do it for iOS:

document.addEventListener("orientationChanged", UpdateOrientation);
function UpdateOrientation(e)
{
switch(e.orientation)
{
case 0: // portrait
case 180: // portrait
// do your thing here
break;
case -90: // landscape
case 90: // landscape
// do your thing here
break;
}
}

Regards,
Scott

TK

unread,
Jun 7, 2011, 2:54:31 PM6/7/11
to phonegap
Thanks for your help but it doesnt work..
Ive added to alert statements at the positions you marked, but nothing
happened.
Android is not working too.
Do i have to use the phonegap.js? Ive referenced it in my HTML-file.
And im using the build service, maybe this is important to know too.
Do you have an example iOS-App so i can test it on my ipod, if it is
working?

best regards

Shazron Abdullah

unread,
Jun 7, 2011, 3:18:48 PM6/7/11
to phon...@googlegroups.com
Wait for the deviceready event. Create a new PhoneGap project, and check out the sample index.html on how its done.

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

TK

unread,
Jun 9, 2011, 5:47:35 AM6/9/11
to phonegap
Well my Code looks like that:

<script type="text/javascript" charset="utf-8" src="phonegap.
0.9.5.1.min.js"></script>

function onBodyLoad()
{
document.addEventListener("deviceready",onDeviceReady,false);
}

function onDeviceReady()
{
document.addEventListener("orientationChanged", UpdateOrientation);
}

function UpdateOrientation(e)
{
switch(e.orientation)
{
case 0: // portrait
case 180: // portrait
document.getElementById("oc1").innerHTML =
window.orientation;
document.getElementById("oc2").innerHTML = 'portrait';
break;
case -90: // landscape
case 90: // landscape
document.getElementById("oc3").innerHTML =
window.orientation;
document.getElementById("oc4").innerHTML = 'landscape';
break;
}
}

But nothing happens :-(.


On 7 Jun., 21:18, Shazron Abdullah <shazron.abdul...@nitobi.com>
wrote:

Giacomo Balli

unread,
Jun 9, 2011, 8:26:24 AM6/9/11
to phonegap
missing a parameter in your orientationChanged listener

TK

unread,
Jun 9, 2011, 2:08:38 PM6/9/11
to phonegap
Thanks for your answer.
But even with that missing parameter, nothing happens.
Not on iphone and not on my android phone.

any other ideas? =(
Message has been deleted

lawwantsin

unread,
Jun 13, 2011, 11:25:30 AM6/13/11
to phon...@googlegroups.com
for anyone combing the internets for a simple orientationchange event that works in phonegap 0.9.5.  here we go.
tested on the simulator and the device.

window.onorientationchange = function() { turn(window.orientation); }

turn = function(o) {
    switch(o) {
      case 0: // portrait, home bottom
      case 180: // portrait, home top
        alert('portrait');
        break;
      case -90: // landscape, home left
      case 90: // landscape, home right
        alert('landscape');
        break;
    }    
}

you can put the window.onorientationchange anywhere inside jquery code, etc and it still works because you're assigning that event on the global window object to a function.  mine works inside a javascriptmvc controller.

now my way of chaning things up is by adding or removing a landscape class to the body tag in the turn function and styling the landscape body differently from the portrait.  it requires some planning, but it's much better than loading a seperate page for landscape and portrait or creating seperate divs.  though you'll have to decide what's best for your app.   

TK

unread,
Jun 14, 2011, 7:10:05 AM6/14/11
to phonegap
thank you very much for your answer, but i have to tell you that
nothing happens..
maybe you can send me the archive where you zipped the files which are
working?
on android native app it is working, but on ipod touch nothing
happens..



On 13 Jun., 17:25, lawwantsin <somethingnoonehasthough...@gmail.com>
wrote:

TK

unread,
Jun 29, 2011, 3:37:34 AM6/29/11
to phonegap
Is there no solution, no-one has an idea?
cmon please!

Shazron Abdullah

unread,
Jun 29, 2011, 4:16:41 AM6/29/11
to phon...@googlegroups.com
I can assure you it works, there is probably something wrong with your setup.
Try the attached index.html - make sure it refers to the right phonegap.js file

index.html

TK

unread,
Jun 30, 2011, 12:20:39 PM6/30/11
to phonegap
...

TK

unread,
Jun 30, 2011, 12:07:01 PM6/30/11
to phonegap
thank you very much for your reply.
well ive tested it now on android 2.3.3 and iOs 4.2.1 (iPod Touch).
first ive tested it as website directly from the browsers.
with the ipod it is working fine, with the android device nothing
happens.
after that ive build a native app with the phonegap build service.
on the android device again nothing happens.
when im starting the native app on ipod touch, it shows me the message
"orientation: Portrait", but after rotating the device nothing
happens.
do im doing something wrong?

regards

On Jun 29, 10:16 am, Shazron Abdullah <shazron.abdul...@nitobi.com>
wrote:
> I can assure you it works, there is probably something wrong with your setup.
> Try the attached index.html - make sure it refers to the right phonegap.js file
>
>  index.html
> 2KViewDownload

Shazron Abdullah

unread,
Jul 1, 2011, 1:44:32 PM7/1/11
to phon...@googlegroups.com
You might want to check out the PhoneGap Build forums and post there, not here, it takes a while before PhoneGap Build supports the bleeding edge release.

TK

unread,
Jul 5, 2011, 10:58:48 AM7/5/11
to phonegap
thank you very much for your reply. ive posted it on phonegap build
forums

On Jul 1, 7:44 pm, Shazron Abdullah <shazron.abdul...@nitobi.com>
wrote:
Reply all
Reply to author
Forward
0 new messages