SajjadZare
SajjadZare

Reputation: 2378

Accelerometer doesn't work on iphone4

I use below code for accelerometer and it works on ipod 3g but doesn't work on iphone4 (doesn't show alert)

.h

#define kAccelerationThreshold      2.2
#define kUpdateInterval         (1.0f/10.0f)

.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIAccelerometer *accel=[UIAccelerometer sharedAccelerometer];
    accel.delegate=self;
    accel.updateInterval=kUpdateInterval;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    if (fabsf(acceleration.x) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold)
    {
         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Shake" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [alert show];

    }
}

Upvotes: 0

Views: 585

Answers (3)

Wex
Wex

Reputation: 4696

Copied your code into a new project - in order to get it to work I had to lower the kAccelerationThreshold value to around 1.5 - which still requires a fairly violent shake.

Only tested it on iPhone 4.

Upvotes: 1

benwong
benwong

Reputation: 2226

accel=updateInterval=kUpdateInterval;

Looks like it should be

accel.updateInterval=kUpdateInterval;

Upvotes: 0

WrightsCS
WrightsCS

Reputation: 50727

Check out the UIAccelerometer Class Reference.

Be sure to also implement the UIAccelerometerDelegate protocol in your header.

Upvotes: 0

Related Questions