diff --git a/Aspects.m b/Aspects.m index c907066..c428228 100644 --- a/Aspects.m +++ b/Aspects.m @@ -393,8 +393,13 @@ static void aspect_swizzleForwardInvocation(Class klass) { // If there is no method, replace will act like class_addMethod. IMP originalImplementation = class_replaceMethod(klass, @selector(forwardInvocation:), (IMP)__ASPECTS_ARE_BEING_CALLED__, "v@:@"); if (originalImplementation) { - class_addMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName), originalImplementation, "v@:@"); - } + BOOL add= class_addMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName), originalImplementation, "v@:@"); + if (!add) { + Method ORIGMethod = class_getInstanceMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName)); + method_setImplementation(ORIGMethod, originalImplementation); + } + } + AspectLog(@"Aspects: %@ is now aspect aware.", NSStringFromClass(klass)); } @@ -438,7 +443,7 @@ static Class aspect_swizzleClassInPlace(Class klass) { NSString *className = NSStringFromClass(klass); _aspect_modifySwizzledClasses(^(NSMutableSet *swizzledClasses) { - if (![swizzledClasses containsObject:className]) { + if (class_getMethodImplementation(klass, @selector(forwardInvocation:)) != (IMP)__ASPECTS_ARE_BEING_CALLED__) { aspect_swizzleForwardInvocation(klass); [swizzledClasses addObject:className]; } diff --git a/AspectsDemo/AspectsDemo.xcodeproj/project.pbxproj b/AspectsDemo/AspectsDemo.xcodeproj/project.pbxproj index ecec2b8..f06149d 100644 --- a/AspectsDemo/AspectsDemo.xcodeproj/project.pbxproj +++ b/AspectsDemo/AspectsDemo.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 1B504D661E5EE21A005F39BA /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1B504D651E5EE21A005F39BA /* Launch Screen.storyboard */; }; + 1B504D681E5EE236005F39BA /* Storyboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1B504D671E5EE236005F39BA /* Storyboard.storyboard */; }; 78573EF519155A2E000D3B00 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573EF419155A2E000D3B00 /* Foundation.framework */; }; 78573EF719155A2E000D3B00 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573EF619155A2E000D3B00 /* CoreGraphics.framework */; }; 78573EF919155A2E000D3B00 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573EF819155A2E000D3B00 /* UIKit.framework */; }; @@ -21,7 +23,6 @@ 78573F1A19155A2E000D3B00 /* AspectsDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 78573F1919155A2E000D3B00 /* AspectsDemoTests.m */; }; 78573F2519155A74000D3B00 /* Aspects.m in Sources */ = {isa = PBXBuildFile; fileRef = 78573F2319155A74000D3B00 /* Aspects.m */; }; 78D7D77119177C8E002EB314 /* AspectsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 78D7D76F19177C8E002EB314 /* AspectsViewController.m */; }; - 78D7D77219177C8E002EB314 /* AspectsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 78D7D77019177C8E002EB314 /* AspectsViewController.xib */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -35,6 +36,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 1B504D651E5EE21A005F39BA /* Launch Screen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = "Launch Screen.storyboard"; sourceTree = ""; }; + 1B504D671E5EE236005F39BA /* Storyboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Storyboard.storyboard; sourceTree = ""; }; 78573EF119155A2E000D3B00 /* AspectsDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AspectsDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 78573EF419155A2E000D3B00 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 78573EF619155A2E000D3B00 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; @@ -55,7 +58,6 @@ 78573F2419155A74000D3B00 /* Aspects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Aspects.h; path = ../../Aspects.h; sourceTree = ""; }; 78D7D76E19177C8E002EB314 /* AspectsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AspectsViewController.h; sourceTree = ""; }; 78D7D76F19177C8E002EB314 /* AspectsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AspectsViewController.m; sourceTree = ""; }; - 78D7D77019177C8E002EB314 /* AspectsViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = AspectsViewController.xib; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -123,7 +125,8 @@ 78573EFB19155A2E000D3B00 /* Supporting Files */, 78D7D76E19177C8E002EB314 /* AspectsViewController.h */, 78D7D76F19177C8E002EB314 /* AspectsViewController.m */, - 78D7D77019177C8E002EB314 /* AspectsViewController.xib */, + 1B504D651E5EE21A005F39BA /* Launch Screen.storyboard */, + 1B504D671E5EE236005F39BA /* Storyboard.storyboard */, ); path = AspectsDemo; sourceTree = ""; @@ -235,7 +238,8 @@ files = ( 78573EFF19155A2E000D3B00 /* InfoPlist.strings in Resources */, 78573F0719155A2E000D3B00 /* Images.xcassets in Resources */, - 78D7D77219177C8E002EB314 /* AspectsViewController.xib in Resources */, + 1B504D661E5EE21A005F39BA /* Launch Screen.storyboard in Resources */, + 1B504D681E5EE236005F39BA /* Storyboard.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -413,6 +417,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemo/AspectsDemo-Prefix.pch"; INFOPLIST_FILE = "AspectsDemo/AspectsDemo-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -426,6 +431,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemo/AspectsDemo-Prefix.pch"; INFOPLIST_FILE = "AspectsDemo/AspectsDemo-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; diff --git a/AspectsDemo/AspectsDemo/AspectsAppDelegate.m b/AspectsDemo/AspectsDemo/AspectsAppDelegate.m index 4c593ad..c1531d5 100644 --- a/AspectsDemo/AspectsDemo/AspectsAppDelegate.m +++ b/AspectsDemo/AspectsDemo/AspectsAppDelegate.m @@ -13,23 +13,15 @@ @implementation AspectsAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - AspectsViewController *aspectsController = [AspectsViewController new]; - self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - self.window.backgroundColor = [UIColor whiteColor]; - self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:aspectsController]; - [self.window makeKeyAndVisible]; - - // Ignore hooks when we are testing. - if (!NSClassFromString(@"XCTestCase")) { - [aspectsController aspect_hookSelector:@selector(buttonPressed:) withOptions:0 usingBlock:^(id info, id sender) { - NSLog(@"Button was pressed by: %@", sender); - } error:NULL]; - - [aspectsController aspect_hookSelector:@selector(viewWillLayoutSubviews) withOptions:0 usingBlock:^{ - NSLog(@"Controller is layouting!"); - } error:NULL]; - } - + [AspectsViewController aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionBefore usingBlock:^ { + NSLog(@"viewDidLoad before"); + }error:nil]; + [AspectsViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id aspectInfo,BOOL animated) { + NSLog(@"viewWillAppear After"); + }error:nil]; + [AspectsViewController aspect_hookSelector:@selector(viewDidAppear:) withOptions:AspectPositionInstead usingBlock:^(id aspectInfo,BOOL animated) { + NSLog(@"viewDidAppear Instead"); + }error:nil]; return YES; } diff --git a/AspectsDemo/AspectsDemo/AspectsDemo-Info.plist b/AspectsDemo/AspectsDemo/AspectsDemo-Info.plist index 5e1b0a5..b5b7231 100644 --- a/AspectsDemo/AspectsDemo/AspectsDemo-Info.plist +++ b/AspectsDemo/AspectsDemo/AspectsDemo-Info.plist @@ -24,6 +24,10 @@ 1.0 LSRequiresIPhoneOS + UILaunchStoryboardName + Launch Screen + UIMainStoryboardFile + Storyboard UIRequiredDeviceCapabilities armv7 diff --git a/AspectsDemo/AspectsDemo/AspectsViewController.h b/AspectsDemo/AspectsDemo/AspectsViewController.h index ca11881..839fbbe 100644 --- a/AspectsDemo/AspectsDemo/AspectsViewController.h +++ b/AspectsDemo/AspectsDemo/AspectsViewController.h @@ -10,6 +10,5 @@ @interface AspectsViewController : UIViewController -- (IBAction)buttonPressed:(id)sender; @end diff --git a/AspectsDemo/AspectsDemo/AspectsViewController.m b/AspectsDemo/AspectsDemo/AspectsViewController.m index 5310493..e2cb106 100644 --- a/AspectsDemo/AspectsDemo/AspectsViewController.m +++ b/AspectsDemo/AspectsDemo/AspectsViewController.m @@ -11,24 +11,18 @@ @implementation AspectsViewController -- (IBAction)buttonPressed:(id)sender { - UIViewController *testController = [[UIImagePickerController alloc] init]; - - testController.modalPresentationStyle = UIModalPresentationFormSheet; - [self presentViewController:testController animated:YES completion:NULL]; +- (void)viewDidLoad { + [super viewDidLoad]; + +} - // We are interested in being notified when the controller is being dismissed. - [testController aspect_hookSelector:@selector(viewWillDisappear:) withOptions:0 usingBlock:^(id info, BOOL animated) { - UIViewController *controller = [info instance]; - if (controller.isBeingDismissed || controller.isMovingFromParentViewController) { - [[[UIAlertView alloc] initWithTitle:@"Popped" message:@"Hello from Aspects" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show]; - } - } error:NULL]; +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; +} - // Hooking dealloc is delicate, only AspectPositionBefore will work here. - [testController aspect_hookSelector:NSSelectorFromString(@"dealloc") withOptions:AspectPositionBefore usingBlock:^(id info) { - NSLog(@"Controller is about to be deallocated: %@", [info instance]); - } error:NULL]; +- (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; } + @end diff --git a/AspectsDemo/AspectsDemo/AspectsViewController.xib b/AspectsDemo/AspectsDemo/AspectsViewController.xib deleted file mode 100644 index 2e4af86..0000000 --- a/AspectsDemo/AspectsDemo/AspectsViewController.xib +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/AspectsDemo/AspectsDemo/Launch Screen.storyboard b/AspectsDemo/AspectsDemo/Launch Screen.storyboard new file mode 100644 index 0000000..472966d --- /dev/null +++ b/AspectsDemo/AspectsDemo/Launch Screen.storyboard @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AspectsDemo/AspectsDemo/Storyboard.storyboard b/AspectsDemo/AspectsDemo/Storyboard.storyboard new file mode 100644 index 0000000..3ea09fe --- /dev/null +++ b/AspectsDemo/AspectsDemo/Storyboard.storyboard @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +