Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. #import "UINavigationController+Consistent.h"
  2. #import <objc/runtime.h>
  3. /// This char is used to add storage for the isPushingViewController property.
  4. static char const * const ObjectTagKey = "ObjectTag";
  5.  
  6. @interface UINavigationController ()
  7. @property (readwrite,getter = isViewTransitionInProgress) BOOL viewTransitionInProgress;
  8.  
  9. @end
  10.  
  11. @implementation UINavigationController (Consistent)
  12.  
  13. - (void)setViewTransitionInProgress:(BOOL)property {
  14. NSNumber *number = [NSNumber numberWithBool:property];
  15. objc_setAssociatedObject(self, ObjectTagKey, number , OBJC_ASSOCIATION_RETAIN);
  16. }
  17.  
  18.  
  19. - (BOOL)isViewTransitionInProgress {
  20. NSNumber *number = objc_getAssociatedObject(self, ObjectTagKey);
  21.  
  22. return [number boolValue];
  23. }
  24.  
  25.  
  26. #pragma mark - Intercept Pop, Push, PopToRootVC
  27. /// @name Intercept Pop, Push, PopToRootVC
  28.  
  29. - (NSArray *)safePopToRootViewControllerAnimated:(BOOL)animated {
  30. if (self.viewTransitionInProgress) return nil;
  31. if (animated) {
  32. self.viewTransitionInProgress = YES;
  33. }
  34. //-- This is not a recursion, due to method swizzling the call below calls the original method.
  35. return [self safePopToRootViewControllerAnimated:animated];
  36.  
  37. }
  38.  
  39.  
  40. - (NSArray *)safePopToViewController:(UIViewController *)viewController animated:(BOOL)animated {
  41. if (self.viewTransitionInProgress) return nil;
  42. if (animated) {
  43. self.viewTransitionInProgress = YES;
  44. }
  45. //-- This is not a recursion, due to method swizzling the call below calls the original method.
  46. return [self safePopToViewController:viewController animated:animated];
  47. }
  48.  
  49.  
  50. - (UIViewController *)safePopViewControllerAnimated:(BOOL)animated {
  51. if (self.viewTransitionInProgress) return nil;
  52. if (animated) {
  53. self.viewTransitionInProgress = YES;
  54. }
  55. //-- This is not a recursion, due to method swizzling the call below calls the original method.
  56. return [self safePopViewControllerAnimated:animated];
  57. }
  58.  
  59.  
  60.  
  61. - (void)safePushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  62. self.delegate = self;
  63. //-- If we are already pushing a view controller, we dont push another one.
  64. if (self.isViewTransitionInProgress == NO) {
  65. //-- This is not a recursion, due to method swizzling the call below calls the original method.
  66. [self safePushViewController:viewController animated:animated];
  67. if (animated) {
  68. self.viewTransitionInProgress = YES;
  69. }
  70. }
  71. }
  72.  
  73.  
  74. // This is confirmed to be App Store safe.
  75. // If you feel uncomfortable to use Private API, you could also use the delegate method navigationController:didShowViewController:animated:.
  76. - (void)safeDidShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  77. //-- This is not a recursion. Due to method swizzling this is calling the original method.
  78. [self safeDidShowViewController:viewController animated:animated];
  79. self.viewTransitionInProgress = NO;
  80. }
  81.  
  82.  
  83. // If the user doesnt complete the swipe-to-go-back gesture, we need to intercept it and set the flag to NO again.
  84. - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  85. id<UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator;
  86. [tc notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  87. self.viewTransitionInProgress = NO;
  88. //--Reenable swipe back gesture.
  89. self.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)viewController;
  90. [self.interactivePopGestureRecognizer setEnabled:YES];
  91. }];
  92. //-- Method swizzling wont work in the case of a delegate so:
  93. //-- forward this method to the original delegate if there is one different than ourselves.
  94. if (navigationController.delegate != self) {
  95. [navigationController.delegate navigationController:navigationController
  96. willShowViewController:viewController
  97. animated:animated];
  98. }
  99. }
  100.  
  101.  
  102. + (void)load {
  103. //-- Exchange the original implementation with our custom one.
  104. method_exchangeImplementations(class_getInstanceMethod(self, @selector(pushViewController:animated:)), class_getInstanceMethod(self, @selector(safePushViewController:animated:)));
  105. method_exchangeImplementations(class_getInstanceMethod(self, @selector(didShowViewController:animated:)), class_getInstanceMethod(self, @selector(safeDidShowViewController:animated:)));
  106. method_exchangeImplementations(class_getInstanceMethod(self, @selector(popViewControllerAnimated:)), class_getInstanceMethod(self, @selector(safePopViewControllerAnimated:)));
  107. method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToRootViewControllerAnimated:)), class_getInstanceMethod(self, @selector(safePopToRootViewControllerAnimated:)));
  108. method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToViewController:animated:)), class_getInstanceMethod(self, @selector(safePopToViewController:animated:)));
  109. }
  110.  
  111. @end
  112.  
  113. NSArray *viewControllers = self.navigationController.viewControllers;
  114.  
  115. NSArray *viewControllers = self.navigationController.viewControllers;
  116.  
  117. for (UIViewController *viewController in viewControllers) {
  118.  
  119. if (viewController.isBeingPresented || viewController.isBeingDismissed) {
  120. // In this case when a pop or push is already in progress, don't perform
  121. // a pop or push on the current view controller. Perhaps check again after
  122. // a delay.
  123. return;
  124. }
  125. }
  126.  
  127. // Else if you make it through the loop uninterrupted, perform push or pop.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement