I had loads of problems getting this to work and I finally gave up and followed jandrea's advice. He suggested subclassing UIWindow and implement the motionEnded there. This is a quote from his post here, look for it quite far down.
First, I subclassed UIWindow. This is
easy peasy. Create a new class file
with an interface such as MotionWindow
: UIWindow (feel free to pick your
own, natch). Add a method like so:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeviceShaken" object:self];
}
}
Change @"DeviceShaken" to the
notification name of your choice. Save
the file.
Now, if you use a MainWindow.xib
(stock Xcode template stuff), go in
there and change the class of your
Window object from UIWindow to
MotionWindow or whatever you called
it. Save the xib. If you set up
UIWindow programmatically, use your
new Window class there instead.
Now your app is using the specialized
UIWindow class. Wherever you want to
be told about a shake, sign up for
them notifications! Like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceShaken) name:@"DeviceShaken" object:nil];
To remove yourself as an observer:
[[NSNotificationCenter defaultCenter] removeObserver:self];