Evgeniy Kleban Evgeniy Kleban - 5 months ago 15
Objective-C Question

Detect when user left viewController

I have controller (news screen), and i need to detect when user leave it. I tried

- (void)viewWillDisappear:(BOOL)animated


, but the problem is, when user tap share button (share in social networks etc.) that method triggers, but after sharing user is still in news screen, therefore its not work.

I also tried

-(void)willMoveToParentViewController:(UIViewController *)parent {


, but it also trigger when user first enter controller, which is wrong (i need to detect leaving only).

How can i detect when user leave controller, but not trigger when he enter "sharing" pop screen?

Answer

Check this it will help you.

UIActivityViewController *conroller=[[UIActivityViewController alloc] initWithActivityItems:@[@"Hello"] applicationActivities:nil];

You can handle the sharing thing in the completion here

[conroller setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError){
      if(!activityError)
         NSLog(@"Shared");
}];

The completion will tell you that the activity was presented so you can handle the activities you want to handle in the completion like this

[self presentViewController:conroller animated:YES completion:^{
       NSLog(@"Activity Appeared"); //Same as viewWillDisappear
}];

Hope this helps.

Comments