Hardik Amal
Hardik Amal

Reputation: 1353

iOS : Handle multiple uilocalnotification with multiple same viewcontroller

I am using UILocalnotification...on receiving the notification i open viewcontroller when the app is in active mode...but if multiple notifications are received at same time...how do i open the multiple viewcontroller...above each other and dismiss them sequentially....I tried opening viewcontroller but receiving this error

Warning: Attempt to present <NotificationViewController: 0x7fc033b43900> on <UINavigationController: 0x7fc031859600> whose view is not in the window hierarchy!

Upvotes: 1

Views: 149

Answers (1)

Yunus Eren G&#252;zel
Yunus Eren G&#252;zel

Reputation: 3088

There is a hack to do this.

write an extension to view controller:

extension UIViewController {
    var lastPresentedViewController: UIViewController {
        guard let presentedViewController = presentedViewController else { return self }
        return presentedViewController.lastPresentedViewController()
    }
}

or objc:

UIViewController+LastPresentedViewController.h:

@interface UIViewController (LastPresentedViewController)
-(UIViewController *)lastPresentedViewController;
@end

UIViewController+LastPresentedViewController.m:

@implementation UIViewController (LastPresentedViewController)
- (UIViewController *)lastPresentedViewController {
    if (self.presentedViewController) {
        return [self.presentedViewController lastPresentedViewController];
    } else {
        return self;
    }
}
@end

when you need to present a view controller from navigationController just call this method like this:

navigationController.lastPresentedViewController.presentViewController(....

if you are already inside navigationController just call lastPresentedViewController.presentViewController(...

Upvotes: 1

Related Questions