Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

motionEnded not called in appDelegate

Tags:

ios

iphone

I want to integrate shake feature throughout the app. So I am doing everything in appDelegate. I need to push a viewController, I am able to push in motionBegan, but i wanted to do it motionEnded. yes motion ended does work in a view controller, but in app delegate it is not being called. Doing as

- (void)applicationDidBecomeActive:(UIApplication *)application {
     [self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder{
    return YES;
}

motionEnded not called

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

motionBegan called

-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if(event.subtype==UIEventSubtypeMotionShake){
        NSLog(@"motionBegan called");
    }
}
like image 405
Raheel Sadiq Avatar asked Nov 12 '22 10:11

Raheel Sadiq


1 Answers

you could basically register your viewController for applicationDidBecomeActiveNotification or any depending on your needs

for example in your viewController's viewDidLoad method you could register it for notification

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(myMethod)
                                                 name:UIApplicationDidBecomeActiveNotification object:nil];

and implement this method in your class, your myMethod will call everytime your application will become active

-(void) myMethod(){
 // do your stuff
}

finally un-register viewController from the notification in dealloc method

-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
like image 191
nsgulliver Avatar answered Nov 15 '22 05:11

nsgulliver