Javascript. Listen for iPhone shake event?
Asked Answered
E

4

9

Is it possible to listen for an iPhone (or any mobile phone with accelerometer) shake event with JavaScript? You know like shaking the iPhone to shuffle the next song in iTunes I want to call a JS function on my website when the iPhone got shaken.

Elwaine answered 4/2, 2010 at 18:38 Comment(0)
C
1

Not as a straight web app, no. If you wrap your web app in PhoneGap, it exposes accelerometer and other app-only features to the site running within it, but the downside is that people have to actually download your app - they can't just browse to a site in Safari.

Cung answered 4/2, 2010 at 18:39 Comment(0)
G
16

In the latest iOS update (4.2) the accelorometer is now accessible from Javascript.

See: http://www.mobilexweb.com/blog/safari-ios-accelerometer-websockets-html5

Grindlay answered 25/11, 2010 at 14:6 Comment(0)
A
10

Here's an elegant implementation of shake detection using the accelerometer if anyone is looking for it:

https://github.com/alexgibson/shake.js

Appurtenant answered 3/4, 2011 at 18:51 Comment(0)
D
4

I just implemented the above in my web app which now has controls that appear on the page only after a device shake occurs. Very easy to implement and cleanly described.

https://github.com/alexgibson/shake.js

It is also worth noting that the iOS simulator's shake gesture doesn't trigger the event but a genuine device shake does.

Domenicadomenico answered 22/3, 2012 at 12:25 Comment(0)
C
1

Not as a straight web app, no. If you wrap your web app in PhoneGap, it exposes accelerometer and other app-only features to the site running within it, but the downside is that people have to actually download your app - they can't just browse to a site in Safari.

Cung answered 4/2, 2010 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.