From bb1479dc94f8142e5d48eb26532e5036cc8679ca Mon Sep 17 00:00:00 2001 From: Suyeol Jeon Date: Mon, 27 Jul 2015 23:42:26 +0900 Subject: [PATCH] Drop JRSwizzle dependency. --- SwipeBack.podspec | 3 +- SwipeBack/UINavigationController+SwipeBack.h | 2 + SwipeBack/UINavigationController+SwipeBack.m | 42 ++++++++++++++------ SwipeBack/UIViewController+SwipeBack.m | 22 +++++----- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/SwipeBack.podspec b/SwipeBack.podspec index dd7ea1b..2d8a21b 100644 --- a/SwipeBack.podspec +++ b/SwipeBack.podspec @@ -10,6 +10,5 @@ Pod::Spec.new do |s| s.platform = :ios, '7.0' s.requires_arc = true s.source_files = 'SwipeBack/*.{h,m}' - s.frameworks = 'UIKit', 'Foundation', 'QuartzCore' - s.dependency 'JRSwizzle', '~> 1.0' + s.frameworks = 'UIKit', 'Foundation' end diff --git a/SwipeBack/UINavigationController+SwipeBack.h b/SwipeBack/UINavigationController+SwipeBack.h index 5eed53e..c47d7a8 100644 --- a/SwipeBack/UINavigationController+SwipeBack.h +++ b/SwipeBack/UINavigationController+SwipeBack.h @@ -24,6 +24,8 @@ #import +void __swipeback_swizzle(Class cls, SEL originalSelector); + @interface UINavigationController (SwipeBack) @property (nonatomic, assign) BOOL swipeBackEnabled; // default is `YES` diff --git a/SwipeBack/UINavigationController+SwipeBack.m b/SwipeBack/UINavigationController+SwipeBack.m index 9fa1651..6f27a00 100644 --- a/SwipeBack/UINavigationController+SwipeBack.m +++ b/SwipeBack/UINavigationController+SwipeBack.m @@ -22,37 +22,53 @@ // SOFTWARE. // - -#import #import - #import "UINavigationController+SwipeBack.h" +void __swipeback_swizzle(Class cls, SEL originalSelector) { + NSString *originalName = NSStringFromSelector(originalSelector); + NSString *alternativeName = [NSString stringWithFormat:@"swizzled_%@", originalName]; + + SEL alternativeSelector = NSSelectorFromString(alternativeName); + + Method originalMethod = class_getInstanceMethod(cls, originalSelector); + Method alternativeMethod = class_getInstanceMethod(cls, alternativeSelector); + + class_addMethod(cls, + originalSelector, + class_getMethodImplementation(cls, originalSelector), + method_getTypeEncoding(originalMethod)); + class_addMethod(cls, + alternativeSelector, + class_getMethodImplementation(cls, alternativeSelector), + method_getTypeEncoding(alternativeMethod)); + + method_exchangeImplementations(class_getInstanceMethod(cls, originalSelector), + class_getInstanceMethod(cls, alternativeSelector)); +} + + @implementation UINavigationController (SwipeBack) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - [self jr_swizzleMethod:@selector(viewDidLoad) - withMethod:@selector(hack_viewDidLoad) - error:nil]; - [self jr_swizzleMethod:@selector(pushViewController:animated:) - withMethod:@selector(hack_pushViewController:animated:) - error:nil]; + __swipeback_swizzle(self, @selector(viewDidLoad)); + __swipeback_swizzle(self, @selector(pushViewController:animated:)); }); } -- (void)hack_viewDidLoad +- (void)swizzled_viewDidLoad { - [self hack_viewDidLoad]; + [self swizzled_viewDidLoad]; self.interactivePopGestureRecognizer.delegate = self.swipeBackEnabled ? self : nil; } -- (void)hack_pushViewController:(UIViewController *)viewController animated:(BOOL)animated +- (void)swizzled_pushViewController:(UIViewController *)viewController animated:(BOOL)animated { - [self hack_pushViewController:viewController animated:animated]; + [self swizzled_pushViewController:viewController animated:animated]; self.interactivePopGestureRecognizer.enabled = NO; } diff --git a/SwipeBack/UIViewController+SwipeBack.m b/SwipeBack/UIViewController+SwipeBack.m index 7275cb1..b44098c 100644 --- a/SwipeBack/UIViewController+SwipeBack.m +++ b/SwipeBack/UIViewController+SwipeBack.m @@ -22,28 +22,32 @@ // SOFTWARE. // - -#import - +#import #import "UINavigationController+SwipeBack.h" #import "UIViewController+SwipeBack.h" - @implementation UIViewController (SwipeBack) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - [self jr_swizzleMethod:@selector(viewDidAppear:) - withMethod:@selector(hack_viewDidAppear:) - error:nil]; + __swipeback_swizzle(self, @selector(viewDidAppear:)); +// [self swizzle:@selector(viewDidAppear:)]; }); } -- (void)hack_viewDidAppear:(BOOL)animated ++ (void)swizzle:(SEL)selector +{ + NSString *name = [NSString stringWithFormat:@"swizzled_%@", NSStringFromSelector(selector)]; + Method m1 = class_getInstanceMethod(self, selector); + Method m2 = class_getInstanceMethod(self, NSSelectorFromString(name)); + method_exchangeImplementations(m1, m2); +} + +- (void)swizzled_viewDidAppear:(BOOL)animated { - [self hack_viewDidAppear:animated]; + [self swizzled_viewDidAppear:animated]; self.navigationController.interactivePopGestureRecognizer.enabled = YES; }