Home > Mobile >  How to present popover view and dismiss in appDelegate Objective-C
How to present popover view and dismiss in appDelegate Objective-C

Time:11-03

I'm not familiar with Objective-C and need help for present popover view controller in appDelegate when user tap the push notification bubble.

I can present popover for the vc I want but can not dismiss it. I think I didn't set the root? Please help. Thanks!

My storyboard flow:

[Navigation Controller][ViewControllerA] -> [ViewControllerB] -> [ViewControllerC]

My code can only show ViewControllerC and the dismiss button doesn't work (to dismiss to ViewControllerB).

I want to be able to tap the close button to dismiss ViewControllerC to ViewControllerB, and tap the close button on B to go to A.

//AppDelegate.m

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;

    
    if ([(userInfo[kGCMMessageIDKey])  isEqual: @"ViewControllerC"]) {
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        ViewControllerC *vc = (ViewControllerC *)[storyboard instantiateViewControllerWithIdentifier:@"ViewControllerC"];
        self.window.rootViewController = vc;
        
    } else {
        //go to another vc
    }

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  // Print full message.
  NSLog(@"%@", userInfo);

  completionHandler();
}


//ViewControllerA.m 
- (void) ViewControllerB_BackButtonPressed {
    [self dismissViewControllerAnimated:YES completion:nil];
}


//ViewControllerB.m
- (void) ViewControllerC_BackButtonPressed {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)backButtonPressed:(id)sender {
    [self.delegate ViewControllerB_BackButtonPressed];
}


//ViewControllerC.m
- (IBAction) backButtonPressed:(id)sender {
    [self.delegate ViewControllerC_BackButtonPressed];
}

CodePudding user response:

If I understood your question correctly, you want entire stack of controllers to show up in response to the notification. In this case you need to initialize them all and wrap in a navigation controller, like this:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *va = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerA"];
UIViewController *vb = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerC"];
UINavigationController *navController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
navController.viewControllers = @[va, vb, vc]; 
window.rootViewController = navController;

CodePudding user response:

I end up skipping ViewControllerB to just present popover C on top of the A.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
withCompletionHandler:(void(^)(void))completionHandler {
         
    ViewControllerA *viewControllerA = [mainStoryboard instantiateViewControllerWithIdentifier:@"ViewControllerA"];
    
    //check top view controller
    UIViewController *topViewController = self.window.rootViewController;
        while (true) {
            if (topViewController.presentedViewController) {
                topViewController = topViewController.presentedViewController;
            } else if ([topViewController isKindOfClass:[UINavigationController class]]) {
                UINavigationController *nav = (UINavigationController *)topViewController;
                topViewController = nav.topViewController;
            } else {
                break;
            }
        }
        
        if (topViewController != viewController) {
            [topViewController dismissViewControllerAnimated:YES completion:nil];
        }
    
        if ([(userInfo[kGCMMessageIDKey])  isEqual: @"ViewControllerC"]) {
                
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            UIViewController *viewControllerC = [mainStoryboard instantiateViewControllerWithIdentifier:@"ViewControllerC"];
            viewControllerC.modalPresentationStyle = UIModalPresentationPopover;

            viewControllerC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
            [self.window.rootViewController presentViewController:viewControllerC animated:YES completion:NULL];
                
        } else {
            //go to another vc
        }
    completionHandler();
}
  • Related