FelixF
FelixF

Reputation: 83

Shake recognition not working on iOS 4, does work on iOS 5

I've integrated a shake recognition in my app. I've put it in my appdelegate to be able to use it throughout the app. It works great on iOS 5, but on iOS 4 it doesn't work.

I'm using the following code in my appdelegate.m:

- (void)applicationDidBecomeActive:(UIApplication *)application {
   [self becomeFirstResponder];
   ....
}

-(BOOL)canBecomeFirstResponder {
   return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"motionBegan");
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"motionEnded");
}

If I run this in the iOS5 simulator and activate the shake gesture I get the NSLog messages. If I run it in the iOS 4.3 simulator it doesn't work. Same thing with real devices.

Upvotes: 2

Views: 1331

Answers (1)

user1248465
user1248465

Reputation: 224

I had the same issue. Try

- (void)viewDidAppear:(BOOL)animated {
   [self becomeFirstResponder];
}

instead of

- (void)applicationDidBecomeActive:(UIApplication *)application {
   [self becomeFirstResponder];
   ....
}

as in Event Handling Guide: Motion Events. This worked for me.

Upvotes: 2

Related Questions