3

How can I programmatically trigger the shake event in iOS?

I've tried the following but it keeps crashing...

+ (void)shake {
    NSLog(@"TEST");

    UIMotionEventProxy *m = [[NSClassFromString(@"UIMotionEvent") alloc] _init];

    m->_subtype = UIEventSubtypeMotionShake;
    m->_shakeState = 1;

    [[[UIApplication sharedApplication] keyWindow] motionBegan:UIEventSubtypeMotionShake withEvent:m];
    [[[UIApplication sharedApplication] keyWindow] motionEnded:UIEventSubtypeMotionShake withEvent:m];
}

What does apple do in the simulator under Hardware > Shake Gesture?

user3558410
  • 59
  • 1
  • 9
  • I dont really understand why this is needed. If you just need to have a vibration you could try this : http://stackoverflow.com/questions/4724980/making-the-iphone-vibrate – jithinroy Jul 21 '14 at 13:57
  • Take a look through this tutorial http://www.ioscreator.com/tutorials/detect-shake-gesture-on-a-device – Wesley Guastella Jul 22 '14 at 13:10

3 Answers3

4

Try to replace

UIMotionEventProxy *m = [[NSClassFromString(@"UIMotionEvent") alloc] _init];

with

UIMotionEventProxy *m = [[UIMotionEventProxy alloc] _init];

I guess it's crushing when NSClassFromString(@"UIMotionEvent") returns nil.

Mark Pervovskiy
  • 1,123
  • 11
  • 18
2

Do you want to use in Jailbreak app or Apple compliant use ?

For your question about simulator, Apple use a private function.

Here is a little explanation:

When you use "Shake Gesture", the simulator call sendButtonEvent:0x3fc

sendButtonEvent is a function in Simulator, who:

  • get frontmostAppPort
  • send a message via sendPurpleEvent or HIDEvent

In a jailbreak application, you can do something like (not tested, but should works):

struct UIEvent {
    int subtype;
    double timestamp;
    int type;
} * event;

bzero(event, sizeof(event));

event->type = UIEventTypeMotion;
event->subtype = UIEventSubtypeMotionShake;
event->timestamp = GSCurrentEventTimestamp();

NSString* bundle = [[NSBundle mainBundle] bundleIdentifier];
mach_port_t port = GSCopyPurpleNamedPort([bundle UTF8String]);

GSEventRecord* record = (GSEventRecord*)event;
GSSendEvent(record, port);
skrew
  • 879
  • 9
  • 16
2

Changing the UIMotionEventProxy class (adding two setter methods) seemed to do the trick. I simply added two methods setShakeState and _setSubtype, shown below.

-(void)setShakeState:(int)fp8 {
    _shakeState = fp8;
}
-(void)_setSubtype:(int)fp8 {
    _subtype = fp8;
}

I then changed my code to the following...

UIMotionEventProxy *m = [[NSClassFromString(@"UIMotionEvent") alloc] _init];

[m setShakeState:1];
[m _setSubtype:UIEventSubtypeMotionShake];

[[UIApplication sharedApplication] sendEvent:m];
[[[UIApplication sharedApplication] keyWindow] motionBegan:UIEventSubtypeMotionShake withEvent:m];
[[[UIApplication sharedApplication] keyWindow] motionEnded:UIEventSubtypeMotionShake withEvent:m];

Seems to be working flawlessly for both simulator and physical device. Here are main and header files available for download if anyone wants to see the full UIMotionEventProxy files.

Henry Harris
  • 161
  • 1
  • 11