'how to Redirect google page after method calling

I am tried to after completed animation redirect to Google page I tried like this ;

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
    [self close];
    [self.audioPlayer2 play];
    [self open];
}
-(void)close
{
    CABasicAnimation *shake1 = [CABasicAnimation animationWithKeyPath:@"position1"];
    [shake1 setDuration:1.6];
    [shake1 setRepeatCount:1];
    [shake1 setAutoreverses:YES];
    
    [shake1 setDelegate:self];
    
    firstView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);
    secondView.frame=CGRectMake(0,230 , self.view.frame.size.width, self.view.frame.size.height/2);
    [firstView.layer addAnimation:shake1 forKey:@"position1"];
    [secondView.layer addAnimation:shake1 forKey:@"position1"];

}
-(void)open
{ [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.co.in"]];
}

But my Problem is Close method not completed but open method is called. But I need after completed Closed method and then call to Open method

Can you tell me what wrong in my code?



Solution 1:[1]

I have changed the your code once check whether your issue resolved

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{

    [self close];
    [self.audioPlayer2 play];

}
-(void)close
{
  [CATransaction begin];
    CABasicAnimation *shake1 = [CABasicAnimation animationWithKeyPath:@"position1"];
    [shake1 setDuration:1.6];
    [shake1 setRepeatCount:1];
    [shake1 setAutoreverses:YES];

    [shake1 setDelegate:self];

    firstView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);
    secondView.frame=CGRectMake(0,230 , self.view.frame.size.width, self.view.frame.size.height/2);
[CATransaction setCompletionBlock:^{ [self open];}];
    [firstView.layer addAnimation:shake1 forKey:@"position1"];
    [secondView.layer addAnimation:shake1 forKey:@"position1"];
 [CATransaction commit];
}
-(void)open
{ [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.co.in"]];
}

Here I have used CATransaction, it has a completion block handler.

Solution 2:[2]

Try putting a completion block:

-(void)close:(void(^)(void))completion
{
    CABasicAnimation *shake1 = [CABasicAnimation animationWithKeyPath:@"position1"];
    [shake1 setDuration:1.6];
    [shake1 setRepeatCount:1];
    [shake1 setAutoreverses:YES];

    [shake1 setDelegate:self];

    firstView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);
    secondView.frame=CGRectMake(0,230 , self.view.frame.size.width, self.view.frame.size.height/2);
    [firstView.layer addAnimation:shake1 forKey:@"position1"];
    [secondView.layer addAnimation:shake1 forKey:@"position1"];

    completion();
}


-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{

    [self close:^{
        [self.audioPlayer2 play];
        [self open];
    }];

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 aj_f