Search code examples
iosuinavigationcontrollermotion-detection

How do I configure my app to detect shake motion events?


So let me clarify first off with showing what code i have implemented

-(BOOL)canBecomeFirstResponder 
{
    return YES;
}

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

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

- (void)viewWillDisappear:(BOOL)animated 
{
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}

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

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

this is all in a UIViewController class

and in - (void) applicationDidFinishLaunching:(UIApplication*)application i have set

[application setApplicationSupportsShakeToEdit:YES];

Yet it does nothing, not nary single motion detected. I'm not sure what else to do. This seems to have worked for many other people, so i am baffled as to why i am different...Could it be because it's through a UINavigationController? or because i load the main application via a plist and my main looks like so

retVal = UIApplicationMain(argc, argv, nil, nil);

I am quite thoroughly stumped.


Solution

  • Thank you all for your responses but i ended up using a different method i accessed the accelerometer in the app delegate and then send a nsnotifcation via the nsnotification center to the current ui navigation controller displayed

    - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:nil];
    
    }