├── InterceptorDemo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── deng.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── deng.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── InterceptorDemo.xcscheme │ └── xcschememanagement.plist ├── InterceptorDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── FirstViewController.h ├── FirstViewController.m ├── FirstViewController.xib ├── Info.plist ├── Interceptor │ ├── Aspects.h │ ├── Aspects.m │ ├── UIViewController+Addition.h │ ├── UIViewController+Addition.m │ ├── ViewControllerInterceptor.h │ └── ViewControllerInterceptor.m ├── OtherViewController.h ├── OtherViewController.m └── main.m ├── InterceptorDemoTests ├── Info.plist └── InterceptorDemoTests.m ├── InterceptorDemoUITests ├── Info.plist └── InterceptorDemoUITests.m └── README.md /InterceptorDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B4A28F2B1DDAD28D009248B3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F2A1DDAD28D009248B3 /* main.m */; }; 11 | B4A28F2E1DDAD28D009248B3 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F2D1DDAD28D009248B3 /* AppDelegate.m */; }; 12 | B4A28F341DDAD28D009248B3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B4A28F321DDAD28D009248B3 /* Main.storyboard */; }; 13 | B4A28F361DDAD28D009248B3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B4A28F351DDAD28D009248B3 /* Assets.xcassets */; }; 14 | B4A28F391DDAD28D009248B3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B4A28F371DDAD28D009248B3 /* LaunchScreen.storyboard */; }; 15 | B4A28F441DDAD28D009248B3 /* InterceptorDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F431DDAD28D009248B3 /* InterceptorDemoTests.m */; }; 16 | B4A28F4F1DDAD28D009248B3 /* InterceptorDemoUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F4E1DDAD28D009248B3 /* InterceptorDemoUITests.m */; }; 17 | B4A28F671DDAEB41009248B3 /* OtherViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F661DDAEB41009248B3 /* OtherViewController.m */; }; 18 | B4A28F6B1DDAEFA6009248B3 /* FirstViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F691DDAEFA6009248B3 /* FirstViewController.m */; }; 19 | B4A28F6C1DDAEFA6009248B3 /* FirstViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = B4A28F6A1DDAEFA6009248B3 /* FirstViewController.xib */; }; 20 | B4A28F721DDAF105009248B3 /* UIViewController+Addition.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F6F1DDAF105009248B3 /* UIViewController+Addition.m */; }; 21 | B4A28F731DDAF105009248B3 /* ViewControllerInterceptor.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F711DDAF105009248B3 /* ViewControllerInterceptor.m */; }; 22 | B4A28F761DDAF267009248B3 /* Aspects.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A28F751DDAF267009248B3 /* Aspects.m */; }; 23 | /* End PBXBuildFile section */ 24 | 25 | /* Begin PBXContainerItemProxy section */ 26 | B4A28F401DDAD28D009248B3 /* PBXContainerItemProxy */ = { 27 | isa = PBXContainerItemProxy; 28 | containerPortal = B4A28F1E1DDAD28D009248B3 /* Project object */; 29 | proxyType = 1; 30 | remoteGlobalIDString = B4A28F251DDAD28D009248B3; 31 | remoteInfo = InterceptorDemo; 32 | }; 33 | B4A28F4B1DDAD28D009248B3 /* PBXContainerItemProxy */ = { 34 | isa = PBXContainerItemProxy; 35 | containerPortal = B4A28F1E1DDAD28D009248B3 /* Project object */; 36 | proxyType = 1; 37 | remoteGlobalIDString = B4A28F251DDAD28D009248B3; 38 | remoteInfo = InterceptorDemo; 39 | }; 40 | /* End PBXContainerItemProxy section */ 41 | 42 | /* Begin PBXFileReference section */ 43 | B4A28F261DDAD28D009248B3 /* InterceptorDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = InterceptorDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 44 | B4A28F2A1DDAD28D009248B3 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 45 | B4A28F2C1DDAD28D009248B3 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 46 | B4A28F2D1DDAD28D009248B3 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 47 | B4A28F331DDAD28D009248B3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 48 | B4A28F351DDAD28D009248B3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 49 | B4A28F381DDAD28D009248B3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 50 | B4A28F3A1DDAD28D009248B3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 51 | B4A28F3F1DDAD28D009248B3 /* InterceptorDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = InterceptorDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 52 | B4A28F431DDAD28D009248B3 /* InterceptorDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = InterceptorDemoTests.m; sourceTree = ""; }; 53 | B4A28F451DDAD28D009248B3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 54 | B4A28F4A1DDAD28D009248B3 /* InterceptorDemoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = InterceptorDemoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 55 | B4A28F4E1DDAD28D009248B3 /* InterceptorDemoUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = InterceptorDemoUITests.m; sourceTree = ""; }; 56 | B4A28F501DDAD28D009248B3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 57 | B4A28F651DDAEB41009248B3 /* OtherViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OtherViewController.h; sourceTree = ""; }; 58 | B4A28F661DDAEB41009248B3 /* OtherViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OtherViewController.m; sourceTree = ""; }; 59 | B4A28F681DDAEFA6009248B3 /* FirstViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirstViewController.h; sourceTree = ""; }; 60 | B4A28F691DDAEFA6009248B3 /* FirstViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirstViewController.m; sourceTree = ""; }; 61 | B4A28F6A1DDAEFA6009248B3 /* FirstViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FirstViewController.xib; sourceTree = ""; }; 62 | B4A28F6E1DDAF105009248B3 /* UIViewController+Addition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+Addition.h"; sourceTree = ""; }; 63 | B4A28F6F1DDAF105009248B3 /* UIViewController+Addition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+Addition.m"; sourceTree = ""; }; 64 | B4A28F701DDAF105009248B3 /* ViewControllerInterceptor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewControllerInterceptor.h; sourceTree = ""; }; 65 | B4A28F711DDAF105009248B3 /* ViewControllerInterceptor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewControllerInterceptor.m; sourceTree = ""; }; 66 | B4A28F741DDAF267009248B3 /* Aspects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Aspects.h; sourceTree = ""; }; 67 | B4A28F751DDAF267009248B3 /* Aspects.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Aspects.m; sourceTree = ""; }; 68 | /* End PBXFileReference section */ 69 | 70 | /* Begin PBXFrameworksBuildPhase section */ 71 | B4A28F231DDAD28D009248B3 /* Frameworks */ = { 72 | isa = PBXFrameworksBuildPhase; 73 | buildActionMask = 2147483647; 74 | files = ( 75 | ); 76 | runOnlyForDeploymentPostprocessing = 0; 77 | }; 78 | B4A28F3C1DDAD28D009248B3 /* Frameworks */ = { 79 | isa = PBXFrameworksBuildPhase; 80 | buildActionMask = 2147483647; 81 | files = ( 82 | ); 83 | runOnlyForDeploymentPostprocessing = 0; 84 | }; 85 | B4A28F471DDAD28D009248B3 /* Frameworks */ = { 86 | isa = PBXFrameworksBuildPhase; 87 | buildActionMask = 2147483647; 88 | files = ( 89 | ); 90 | runOnlyForDeploymentPostprocessing = 0; 91 | }; 92 | /* End PBXFrameworksBuildPhase section */ 93 | 94 | /* Begin PBXGroup section */ 95 | B4A28F1D1DDAD28D009248B3 = { 96 | isa = PBXGroup; 97 | children = ( 98 | B4A28F281DDAD28D009248B3 /* InterceptorDemo */, 99 | B4A28F421DDAD28D009248B3 /* InterceptorDemoTests */, 100 | B4A28F4D1DDAD28D009248B3 /* InterceptorDemoUITests */, 101 | B4A28F271DDAD28D009248B3 /* Products */, 102 | ); 103 | sourceTree = ""; 104 | }; 105 | B4A28F271DDAD28D009248B3 /* Products */ = { 106 | isa = PBXGroup; 107 | children = ( 108 | B4A28F261DDAD28D009248B3 /* InterceptorDemo.app */, 109 | B4A28F3F1DDAD28D009248B3 /* InterceptorDemoTests.xctest */, 110 | B4A28F4A1DDAD28D009248B3 /* InterceptorDemoUITests.xctest */, 111 | ); 112 | name = Products; 113 | sourceTree = ""; 114 | }; 115 | B4A28F281DDAD28D009248B3 /* InterceptorDemo */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | B4A28F6D1DDAF105009248B3 /* Interceptor */, 119 | B4A28F2C1DDAD28D009248B3 /* AppDelegate.h */, 120 | B4A28F2D1DDAD28D009248B3 /* AppDelegate.m */, 121 | B4A28F681DDAEFA6009248B3 /* FirstViewController.h */, 122 | B4A28F691DDAEFA6009248B3 /* FirstViewController.m */, 123 | B4A28F6A1DDAEFA6009248B3 /* FirstViewController.xib */, 124 | B4A28F651DDAEB41009248B3 /* OtherViewController.h */, 125 | B4A28F661DDAEB41009248B3 /* OtherViewController.m */, 126 | B4A28F321DDAD28D009248B3 /* Main.storyboard */, 127 | B4A28F351DDAD28D009248B3 /* Assets.xcassets */, 128 | B4A28F371DDAD28D009248B3 /* LaunchScreen.storyboard */, 129 | B4A28F3A1DDAD28D009248B3 /* Info.plist */, 130 | B4A28F291DDAD28D009248B3 /* Supporting Files */, 131 | ); 132 | path = InterceptorDemo; 133 | sourceTree = ""; 134 | }; 135 | B4A28F291DDAD28D009248B3 /* Supporting Files */ = { 136 | isa = PBXGroup; 137 | children = ( 138 | B4A28F2A1DDAD28D009248B3 /* main.m */, 139 | ); 140 | name = "Supporting Files"; 141 | sourceTree = ""; 142 | }; 143 | B4A28F421DDAD28D009248B3 /* InterceptorDemoTests */ = { 144 | isa = PBXGroup; 145 | children = ( 146 | B4A28F431DDAD28D009248B3 /* InterceptorDemoTests.m */, 147 | B4A28F451DDAD28D009248B3 /* Info.plist */, 148 | ); 149 | path = InterceptorDemoTests; 150 | sourceTree = ""; 151 | }; 152 | B4A28F4D1DDAD28D009248B3 /* InterceptorDemoUITests */ = { 153 | isa = PBXGroup; 154 | children = ( 155 | B4A28F4E1DDAD28D009248B3 /* InterceptorDemoUITests.m */, 156 | B4A28F501DDAD28D009248B3 /* Info.plist */, 157 | ); 158 | path = InterceptorDemoUITests; 159 | sourceTree = ""; 160 | }; 161 | B4A28F6D1DDAF105009248B3 /* Interceptor */ = { 162 | isa = PBXGroup; 163 | children = ( 164 | B4A28F741DDAF267009248B3 /* Aspects.h */, 165 | B4A28F751DDAF267009248B3 /* Aspects.m */, 166 | B4A28F6E1DDAF105009248B3 /* UIViewController+Addition.h */, 167 | B4A28F6F1DDAF105009248B3 /* UIViewController+Addition.m */, 168 | B4A28F701DDAF105009248B3 /* ViewControllerInterceptor.h */, 169 | B4A28F711DDAF105009248B3 /* ViewControllerInterceptor.m */, 170 | ); 171 | path = Interceptor; 172 | sourceTree = ""; 173 | }; 174 | /* End PBXGroup section */ 175 | 176 | /* Begin PBXNativeTarget section */ 177 | B4A28F251DDAD28D009248B3 /* InterceptorDemo */ = { 178 | isa = PBXNativeTarget; 179 | buildConfigurationList = B4A28F531DDAD28D009248B3 /* Build configuration list for PBXNativeTarget "InterceptorDemo" */; 180 | buildPhases = ( 181 | B4A28F221DDAD28D009248B3 /* Sources */, 182 | B4A28F231DDAD28D009248B3 /* Frameworks */, 183 | B4A28F241DDAD28D009248B3 /* Resources */, 184 | ); 185 | buildRules = ( 186 | ); 187 | dependencies = ( 188 | ); 189 | name = InterceptorDemo; 190 | productName = InterceptorDemo; 191 | productReference = B4A28F261DDAD28D009248B3 /* InterceptorDemo.app */; 192 | productType = "com.apple.product-type.application"; 193 | }; 194 | B4A28F3E1DDAD28D009248B3 /* InterceptorDemoTests */ = { 195 | isa = PBXNativeTarget; 196 | buildConfigurationList = B4A28F561DDAD28D009248B3 /* Build configuration list for PBXNativeTarget "InterceptorDemoTests" */; 197 | buildPhases = ( 198 | B4A28F3B1DDAD28D009248B3 /* Sources */, 199 | B4A28F3C1DDAD28D009248B3 /* Frameworks */, 200 | B4A28F3D1DDAD28D009248B3 /* Resources */, 201 | ); 202 | buildRules = ( 203 | ); 204 | dependencies = ( 205 | B4A28F411DDAD28D009248B3 /* PBXTargetDependency */, 206 | ); 207 | name = InterceptorDemoTests; 208 | productName = InterceptorDemoTests; 209 | productReference = B4A28F3F1DDAD28D009248B3 /* InterceptorDemoTests.xctest */; 210 | productType = "com.apple.product-type.bundle.unit-test"; 211 | }; 212 | B4A28F491DDAD28D009248B3 /* InterceptorDemoUITests */ = { 213 | isa = PBXNativeTarget; 214 | buildConfigurationList = B4A28F591DDAD28D009248B3 /* Build configuration list for PBXNativeTarget "InterceptorDemoUITests" */; 215 | buildPhases = ( 216 | B4A28F461DDAD28D009248B3 /* Sources */, 217 | B4A28F471DDAD28D009248B3 /* Frameworks */, 218 | B4A28F481DDAD28D009248B3 /* Resources */, 219 | ); 220 | buildRules = ( 221 | ); 222 | dependencies = ( 223 | B4A28F4C1DDAD28D009248B3 /* PBXTargetDependency */, 224 | ); 225 | name = InterceptorDemoUITests; 226 | productName = InterceptorDemoUITests; 227 | productReference = B4A28F4A1DDAD28D009248B3 /* InterceptorDemoUITests.xctest */; 228 | productType = "com.apple.product-type.bundle.ui-testing"; 229 | }; 230 | /* End PBXNativeTarget section */ 231 | 232 | /* Begin PBXProject section */ 233 | B4A28F1E1DDAD28D009248B3 /* Project object */ = { 234 | isa = PBXProject; 235 | attributes = { 236 | LastUpgradeCheck = 0800; 237 | ORGANIZATIONNAME = dengyonghao; 238 | TargetAttributes = { 239 | B4A28F251DDAD28D009248B3 = { 240 | CreatedOnToolsVersion = 8.0; 241 | ProvisioningStyle = Automatic; 242 | }; 243 | B4A28F3E1DDAD28D009248B3 = { 244 | CreatedOnToolsVersion = 8.0; 245 | ProvisioningStyle = Automatic; 246 | TestTargetID = B4A28F251DDAD28D009248B3; 247 | }; 248 | B4A28F491DDAD28D009248B3 = { 249 | CreatedOnToolsVersion = 8.0; 250 | ProvisioningStyle = Automatic; 251 | TestTargetID = B4A28F251DDAD28D009248B3; 252 | }; 253 | }; 254 | }; 255 | buildConfigurationList = B4A28F211DDAD28D009248B3 /* Build configuration list for PBXProject "InterceptorDemo" */; 256 | compatibilityVersion = "Xcode 3.2"; 257 | developmentRegion = English; 258 | hasScannedForEncodings = 0; 259 | knownRegions = ( 260 | en, 261 | Base, 262 | ); 263 | mainGroup = B4A28F1D1DDAD28D009248B3; 264 | productRefGroup = B4A28F271DDAD28D009248B3 /* Products */; 265 | projectDirPath = ""; 266 | projectRoot = ""; 267 | targets = ( 268 | B4A28F251DDAD28D009248B3 /* InterceptorDemo */, 269 | B4A28F3E1DDAD28D009248B3 /* InterceptorDemoTests */, 270 | B4A28F491DDAD28D009248B3 /* InterceptorDemoUITests */, 271 | ); 272 | }; 273 | /* End PBXProject section */ 274 | 275 | /* Begin PBXResourcesBuildPhase section */ 276 | B4A28F241DDAD28D009248B3 /* Resources */ = { 277 | isa = PBXResourcesBuildPhase; 278 | buildActionMask = 2147483647; 279 | files = ( 280 | B4A28F391DDAD28D009248B3 /* LaunchScreen.storyboard in Resources */, 281 | B4A28F361DDAD28D009248B3 /* Assets.xcassets in Resources */, 282 | B4A28F341DDAD28D009248B3 /* Main.storyboard in Resources */, 283 | B4A28F6C1DDAEFA6009248B3 /* FirstViewController.xib in Resources */, 284 | ); 285 | runOnlyForDeploymentPostprocessing = 0; 286 | }; 287 | B4A28F3D1DDAD28D009248B3 /* Resources */ = { 288 | isa = PBXResourcesBuildPhase; 289 | buildActionMask = 2147483647; 290 | files = ( 291 | ); 292 | runOnlyForDeploymentPostprocessing = 0; 293 | }; 294 | B4A28F481DDAD28D009248B3 /* Resources */ = { 295 | isa = PBXResourcesBuildPhase; 296 | buildActionMask = 2147483647; 297 | files = ( 298 | ); 299 | runOnlyForDeploymentPostprocessing = 0; 300 | }; 301 | /* End PBXResourcesBuildPhase section */ 302 | 303 | /* Begin PBXSourcesBuildPhase section */ 304 | B4A28F221DDAD28D009248B3 /* Sources */ = { 305 | isa = PBXSourcesBuildPhase; 306 | buildActionMask = 2147483647; 307 | files = ( 308 | B4A28F2E1DDAD28D009248B3 /* AppDelegate.m in Sources */, 309 | B4A28F761DDAF267009248B3 /* Aspects.m in Sources */, 310 | B4A28F731DDAF105009248B3 /* ViewControllerInterceptor.m in Sources */, 311 | B4A28F2B1DDAD28D009248B3 /* main.m in Sources */, 312 | B4A28F671DDAEB41009248B3 /* OtherViewController.m in Sources */, 313 | B4A28F721DDAF105009248B3 /* UIViewController+Addition.m in Sources */, 314 | B4A28F6B1DDAEFA6009248B3 /* FirstViewController.m in Sources */, 315 | ); 316 | runOnlyForDeploymentPostprocessing = 0; 317 | }; 318 | B4A28F3B1DDAD28D009248B3 /* Sources */ = { 319 | isa = PBXSourcesBuildPhase; 320 | buildActionMask = 2147483647; 321 | files = ( 322 | B4A28F441DDAD28D009248B3 /* InterceptorDemoTests.m in Sources */, 323 | ); 324 | runOnlyForDeploymentPostprocessing = 0; 325 | }; 326 | B4A28F461DDAD28D009248B3 /* Sources */ = { 327 | isa = PBXSourcesBuildPhase; 328 | buildActionMask = 2147483647; 329 | files = ( 330 | B4A28F4F1DDAD28D009248B3 /* InterceptorDemoUITests.m in Sources */, 331 | ); 332 | runOnlyForDeploymentPostprocessing = 0; 333 | }; 334 | /* End PBXSourcesBuildPhase section */ 335 | 336 | /* Begin PBXTargetDependency section */ 337 | B4A28F411DDAD28D009248B3 /* PBXTargetDependency */ = { 338 | isa = PBXTargetDependency; 339 | target = B4A28F251DDAD28D009248B3 /* InterceptorDemo */; 340 | targetProxy = B4A28F401DDAD28D009248B3 /* PBXContainerItemProxy */; 341 | }; 342 | B4A28F4C1DDAD28D009248B3 /* PBXTargetDependency */ = { 343 | isa = PBXTargetDependency; 344 | target = B4A28F251DDAD28D009248B3 /* InterceptorDemo */; 345 | targetProxy = B4A28F4B1DDAD28D009248B3 /* PBXContainerItemProxy */; 346 | }; 347 | /* End PBXTargetDependency section */ 348 | 349 | /* Begin PBXVariantGroup section */ 350 | B4A28F321DDAD28D009248B3 /* Main.storyboard */ = { 351 | isa = PBXVariantGroup; 352 | children = ( 353 | B4A28F331DDAD28D009248B3 /* Base */, 354 | ); 355 | name = Main.storyboard; 356 | sourceTree = ""; 357 | }; 358 | B4A28F371DDAD28D009248B3 /* LaunchScreen.storyboard */ = { 359 | isa = PBXVariantGroup; 360 | children = ( 361 | B4A28F381DDAD28D009248B3 /* Base */, 362 | ); 363 | name = LaunchScreen.storyboard; 364 | sourceTree = ""; 365 | }; 366 | /* End PBXVariantGroup section */ 367 | 368 | /* Begin XCBuildConfiguration section */ 369 | B4A28F511DDAD28D009248B3 /* Debug */ = { 370 | isa = XCBuildConfiguration; 371 | buildSettings = { 372 | ALWAYS_SEARCH_USER_PATHS = NO; 373 | CLANG_ANALYZER_NONNULL = YES; 374 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 375 | CLANG_CXX_LIBRARY = "libc++"; 376 | CLANG_ENABLE_MODULES = YES; 377 | CLANG_ENABLE_OBJC_ARC = YES; 378 | CLANG_WARN_BOOL_CONVERSION = YES; 379 | CLANG_WARN_CONSTANT_CONVERSION = YES; 380 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 381 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 382 | CLANG_WARN_EMPTY_BODY = YES; 383 | CLANG_WARN_ENUM_CONVERSION = YES; 384 | CLANG_WARN_INFINITE_RECURSION = YES; 385 | CLANG_WARN_INT_CONVERSION = YES; 386 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 387 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 388 | CLANG_WARN_UNREACHABLE_CODE = YES; 389 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 390 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 391 | COPY_PHASE_STRIP = NO; 392 | DEBUG_INFORMATION_FORMAT = dwarf; 393 | ENABLE_STRICT_OBJC_MSGSEND = YES; 394 | ENABLE_TESTABILITY = YES; 395 | GCC_C_LANGUAGE_STANDARD = gnu99; 396 | GCC_DYNAMIC_NO_PIC = NO; 397 | GCC_NO_COMMON_BLOCKS = YES; 398 | GCC_OPTIMIZATION_LEVEL = 0; 399 | GCC_PREPROCESSOR_DEFINITIONS = ( 400 | "DEBUG=1", 401 | "$(inherited)", 402 | ); 403 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 404 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 405 | GCC_WARN_UNDECLARED_SELECTOR = YES; 406 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 407 | GCC_WARN_UNUSED_FUNCTION = YES; 408 | GCC_WARN_UNUSED_VARIABLE = YES; 409 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 410 | MTL_ENABLE_DEBUG_INFO = YES; 411 | ONLY_ACTIVE_ARCH = YES; 412 | SDKROOT = iphoneos; 413 | }; 414 | name = Debug; 415 | }; 416 | B4A28F521DDAD28D009248B3 /* Release */ = { 417 | isa = XCBuildConfiguration; 418 | buildSettings = { 419 | ALWAYS_SEARCH_USER_PATHS = NO; 420 | CLANG_ANALYZER_NONNULL = YES; 421 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 422 | CLANG_CXX_LIBRARY = "libc++"; 423 | CLANG_ENABLE_MODULES = YES; 424 | CLANG_ENABLE_OBJC_ARC = YES; 425 | CLANG_WARN_BOOL_CONVERSION = YES; 426 | CLANG_WARN_CONSTANT_CONVERSION = YES; 427 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 428 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 429 | CLANG_WARN_EMPTY_BODY = YES; 430 | CLANG_WARN_ENUM_CONVERSION = YES; 431 | CLANG_WARN_INFINITE_RECURSION = YES; 432 | CLANG_WARN_INT_CONVERSION = YES; 433 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 434 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 435 | CLANG_WARN_UNREACHABLE_CODE = YES; 436 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 437 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 438 | COPY_PHASE_STRIP = NO; 439 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 440 | ENABLE_NS_ASSERTIONS = NO; 441 | ENABLE_STRICT_OBJC_MSGSEND = YES; 442 | GCC_C_LANGUAGE_STANDARD = gnu99; 443 | GCC_NO_COMMON_BLOCKS = YES; 444 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 445 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 446 | GCC_WARN_UNDECLARED_SELECTOR = YES; 447 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 448 | GCC_WARN_UNUSED_FUNCTION = YES; 449 | GCC_WARN_UNUSED_VARIABLE = YES; 450 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 451 | MTL_ENABLE_DEBUG_INFO = NO; 452 | SDKROOT = iphoneos; 453 | VALIDATE_PRODUCT = YES; 454 | }; 455 | name = Release; 456 | }; 457 | B4A28F541DDAD28D009248B3 /* Debug */ = { 458 | isa = XCBuildConfiguration; 459 | buildSettings = { 460 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 461 | INFOPLIST_FILE = InterceptorDemo/Info.plist; 462 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 463 | PRODUCT_BUNDLE_IDENTIFIER = com.dengyonghao.InterceptorDemo; 464 | PRODUCT_NAME = "$(TARGET_NAME)"; 465 | }; 466 | name = Debug; 467 | }; 468 | B4A28F551DDAD28D009248B3 /* Release */ = { 469 | isa = XCBuildConfiguration; 470 | buildSettings = { 471 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 472 | INFOPLIST_FILE = InterceptorDemo/Info.plist; 473 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 474 | PRODUCT_BUNDLE_IDENTIFIER = com.dengyonghao.InterceptorDemo; 475 | PRODUCT_NAME = "$(TARGET_NAME)"; 476 | }; 477 | name = Release; 478 | }; 479 | B4A28F571DDAD28D009248B3 /* Debug */ = { 480 | isa = XCBuildConfiguration; 481 | buildSettings = { 482 | BUNDLE_LOADER = "$(TEST_HOST)"; 483 | INFOPLIST_FILE = InterceptorDemoTests/Info.plist; 484 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 485 | PRODUCT_BUNDLE_IDENTIFIER = com.dengyonghao.InterceptorDemoTests; 486 | PRODUCT_NAME = "$(TARGET_NAME)"; 487 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/InterceptorDemo.app/InterceptorDemo"; 488 | }; 489 | name = Debug; 490 | }; 491 | B4A28F581DDAD28D009248B3 /* Release */ = { 492 | isa = XCBuildConfiguration; 493 | buildSettings = { 494 | BUNDLE_LOADER = "$(TEST_HOST)"; 495 | INFOPLIST_FILE = InterceptorDemoTests/Info.plist; 496 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 497 | PRODUCT_BUNDLE_IDENTIFIER = com.dengyonghao.InterceptorDemoTests; 498 | PRODUCT_NAME = "$(TARGET_NAME)"; 499 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/InterceptorDemo.app/InterceptorDemo"; 500 | }; 501 | name = Release; 502 | }; 503 | B4A28F5A1DDAD28D009248B3 /* Debug */ = { 504 | isa = XCBuildConfiguration; 505 | buildSettings = { 506 | INFOPLIST_FILE = InterceptorDemoUITests/Info.plist; 507 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 508 | PRODUCT_BUNDLE_IDENTIFIER = com.dengyonghao.InterceptorDemoUITests; 509 | PRODUCT_NAME = "$(TARGET_NAME)"; 510 | TEST_TARGET_NAME = InterceptorDemo; 511 | }; 512 | name = Debug; 513 | }; 514 | B4A28F5B1DDAD28D009248B3 /* Release */ = { 515 | isa = XCBuildConfiguration; 516 | buildSettings = { 517 | INFOPLIST_FILE = InterceptorDemoUITests/Info.plist; 518 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 519 | PRODUCT_BUNDLE_IDENTIFIER = com.dengyonghao.InterceptorDemoUITests; 520 | PRODUCT_NAME = "$(TARGET_NAME)"; 521 | TEST_TARGET_NAME = InterceptorDemo; 522 | }; 523 | name = Release; 524 | }; 525 | /* End XCBuildConfiguration section */ 526 | 527 | /* Begin XCConfigurationList section */ 528 | B4A28F211DDAD28D009248B3 /* Build configuration list for PBXProject "InterceptorDemo" */ = { 529 | isa = XCConfigurationList; 530 | buildConfigurations = ( 531 | B4A28F511DDAD28D009248B3 /* Debug */, 532 | B4A28F521DDAD28D009248B3 /* Release */, 533 | ); 534 | defaultConfigurationIsVisible = 0; 535 | defaultConfigurationName = Release; 536 | }; 537 | B4A28F531DDAD28D009248B3 /* Build configuration list for PBXNativeTarget "InterceptorDemo" */ = { 538 | isa = XCConfigurationList; 539 | buildConfigurations = ( 540 | B4A28F541DDAD28D009248B3 /* Debug */, 541 | B4A28F551DDAD28D009248B3 /* Release */, 542 | ); 543 | defaultConfigurationIsVisible = 0; 544 | }; 545 | B4A28F561DDAD28D009248B3 /* Build configuration list for PBXNativeTarget "InterceptorDemoTests" */ = { 546 | isa = XCConfigurationList; 547 | buildConfigurations = ( 548 | B4A28F571DDAD28D009248B3 /* Debug */, 549 | B4A28F581DDAD28D009248B3 /* Release */, 550 | ); 551 | defaultConfigurationIsVisible = 0; 552 | }; 553 | B4A28F591DDAD28D009248B3 /* Build configuration list for PBXNativeTarget "InterceptorDemoUITests" */ = { 554 | isa = XCConfigurationList; 555 | buildConfigurations = ( 556 | B4A28F5A1DDAD28D009248B3 /* Debug */, 557 | B4A28F5B1DDAD28D009248B3 /* Release */, 558 | ); 559 | defaultConfigurationIsVisible = 0; 560 | }; 561 | /* End XCConfigurationList section */ 562 | }; 563 | rootObject = B4A28F1E1DDAD28D009248B3 /* Project object */; 564 | } 565 | -------------------------------------------------------------------------------- /InterceptorDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /InterceptorDemo.xcodeproj/project.xcworkspace/xcuserdata/deng.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dengyhgit/InterceptorDemo/937cb41bfe03f8287abc6c8986048b26f8d58434/InterceptorDemo.xcodeproj/project.xcworkspace/xcuserdata/deng.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /InterceptorDemo.xcodeproj/xcuserdata/deng.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 20 | 21 | 22 | 24 | 36 | 37 | 38 | 40 | 52 | 53 | 54 | 56 | 68 | 69 | 70 | 72 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /InterceptorDemo.xcodeproj/xcuserdata/deng.xcuserdatad/xcschemes/InterceptorDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 89 | 90 | 91 | 92 | 93 | 94 | 100 | 102 | 108 | 109 | 110 | 111 | 113 | 114 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /InterceptorDemo.xcodeproj/xcuserdata/deng.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | InterceptorDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | B4A28F251DDAD28D009248B3 16 | 17 | primary 18 | 19 | 20 | B4A28F3E1DDAD28D009248B3 21 | 22 | primary 23 | 24 | 25 | B4A28F491DDAD28D009248B3 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /InterceptorDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /InterceptorDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "FirstViewController.h" 11 | 12 | @interface AppDelegate () 13 | 14 | @end 15 | 16 | @implementation AppDelegate 17 | 18 | 19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | // Override point for customization after application launch. 21 | FirstViewController *rootCtl = [[FirstViewController alloc] init]; 22 | UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootCtl]; 23 | self.window.rootViewController = nav; 24 | return YES; 25 | } 26 | 27 | 28 | - (void)applicationWillResignActive:(UIApplication *)application { 29 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 30 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 31 | } 32 | 33 | 34 | - (void)applicationDidEnterBackground:(UIApplication *)application { 35 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 36 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 37 | } 38 | 39 | 40 | - (void)applicationWillEnterForeground:(UIApplication *)application { 41 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 42 | } 43 | 44 | 45 | - (void)applicationDidBecomeActive:(UIApplication *)application { 46 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 47 | } 48 | 49 | 50 | - (void)applicationWillTerminate:(UIApplication *)application { 51 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 52 | } 53 | 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /InterceptorDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /InterceptorDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /InterceptorDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /InterceptorDemo/FirstViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // FirstViewController.h 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface FirstViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /InterceptorDemo/FirstViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // FirstViewController.m 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import "FirstViewController.h" 10 | #import "OtherViewController.h" 11 | #import "UIViewController+Addition.h" 12 | 13 | @interface FirstViewController () 14 | 15 | @end 16 | 17 | @implementation FirstViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | } 22 | 23 | - (void)didReceiveMemoryWarning { 24 | [super didReceiveMemoryWarning]; 25 | } 26 | 27 | - (void)viewWillAppear:(BOOL)animated { 28 | [super viewWillAppear:animated]; 29 | NSLog(@"original viewWillAppear:"); 30 | } 31 | 32 | - (IBAction)OtherView:(id)sender { 33 | OtherViewController *vc = [[OtherViewController alloc] init]; 34 | vc.disabledInterceptor = YES; 35 | [self.navigationController pushViewController:vc animated:YES]; 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /InterceptorDemo/FirstViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /InterceptorDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /InterceptorDemo/Interceptor/Aspects.h: -------------------------------------------------------------------------------- 1 | // 2 | // Aspects.h 3 | // Aspects - A delightful, simple library for aspect oriented programming. 4 | // 5 | // Copyright (c) 2014 Peter Steinberger. Licensed under the MIT license. 6 | // 7 | 8 | #import 9 | 10 | typedef NS_OPTIONS(NSUInteger, AspectOptions) { 11 | AspectPositionAfter = 0, /// Called after the original implementation (default) 12 | AspectPositionInstead = 1, /// Will replace the original implementation. 13 | AspectPositionBefore = 2, /// Called before the original implementation. 14 | 15 | AspectOptionAutomaticRemoval = 1 << 3 /// Will remove the hook after the first execution. 16 | }; 17 | 18 | /// Opaque Aspect Token that allows to deregister the hook. 19 | @protocol AspectToken 20 | 21 | /// Deregisters an aspect. 22 | /// @return YES if deregistration is successful, otherwise NO. 23 | - (BOOL)remove; 24 | 25 | @end 26 | 27 | /// The AspectInfo protocol is the first parameter of our block syntax. 28 | @protocol AspectInfo 29 | 30 | /// The instance that is currently hooked. 31 | - (id)instance; 32 | 33 | /// The original invocation of the hooked method. 34 | - (NSInvocation *)originalInvocation; 35 | 36 | /// All method arguments, boxed. This is lazily evaluated. 37 | - (NSArray *)arguments; 38 | 39 | @end 40 | 41 | /** 42 | Aspects uses Objective-C message forwarding to hook into messages. This will create some overhead. Don't add aspects to methods that are called a lot. Aspects is meant for view/controller code that is not called a 1000 times per second. 43 | 44 | Adding aspects returns an opaque token which can be used to deregister again. All calls are thread safe. 45 | */ 46 | @interface NSObject (Aspects) 47 | 48 | /// Adds a block of code before/instead/after the current `selector` for a specific class. 49 | /// 50 | /// @param block Aspects replicates the type signature of the method being hooked. 51 | /// The first parameter will be `id`, followed by all parameters of the method. 52 | /// These parameters are optional and will be filled to match the block signature. 53 | /// You can even use an empty block, or one that simple gets `id`. 54 | /// 55 | /// @note Hooking static methods is not supported. 56 | /// @return A token which allows to later deregister the aspect. 57 | + (id)aspect_hookSelector:(SEL)selector 58 | withOptions:(AspectOptions)options 59 | usingBlock:(id)block 60 | error:(NSError **)error; 61 | 62 | /// Adds a block of code before/instead/after the current `selector` for a specific instance. 63 | - (id)aspect_hookSelector:(SEL)selector 64 | withOptions:(AspectOptions)options 65 | usingBlock:(id)block 66 | error:(NSError **)error; 67 | 68 | @end 69 | 70 | 71 | typedef NS_ENUM(NSUInteger, AspectErrorCode) { 72 | AspectErrorSelectorBlacklisted, /// Selectors like release, retain, autorelease are blacklisted. 73 | AspectErrorDoesNotRespondToSelector, /// Selector could not be found. 74 | AspectErrorSelectorDeallocPosition, /// When hooking dealloc, only AspectPositionBefore is allowed. 75 | AspectErrorSelectorAlreadyHookedInClassHierarchy, /// Statically hooking the same method in subclasses is not allowed. 76 | AspectErrorFailedToAllocateClassPair, /// The runtime failed creating a class pair. 77 | AspectErrorMissingBlockSignature, /// The block misses compile time signature info and can't be called. 78 | AspectErrorIncompatibleBlockSignature, /// The block signature does not match the method or is too large. 79 | 80 | AspectErrorRemoveObjectAlreadyDeallocated = 100 /// (for removing) The object hooked is already deallocated. 81 | }; 82 | 83 | extern NSString *const AspectErrorDomain; 84 | -------------------------------------------------------------------------------- /InterceptorDemo/Interceptor/Aspects.m: -------------------------------------------------------------------------------- 1 | // 2 | // Aspects.m 3 | // Aspects - A delightful, simple library for aspect oriented programming. 4 | // 5 | // Copyright (c) 2014 Peter Steinberger. Licensed under the MIT license. 6 | // 7 | 8 | #import "Aspects.h" 9 | #import 10 | #import 11 | #import 12 | 13 | #define AspectLog(...) 14 | //#define AspectLog(...) do { NSLog(__VA_ARGS__); }while(0) 15 | #define AspectLogError(...) do { NSLog(__VA_ARGS__); }while(0) 16 | 17 | // Block internals. 18 | typedef NS_OPTIONS(int, AspectBlockFlags) { 19 | AspectBlockFlagsHasCopyDisposeHelpers = (1 << 25), 20 | AspectBlockFlagsHasSignature = (1 << 30) 21 | }; 22 | typedef struct _AspectBlock { 23 | __unused Class isa; 24 | AspectBlockFlags flags; 25 | __unused int reserved; 26 | void (__unused *invoke)(struct _AspectBlock *block, ...); 27 | struct { 28 | unsigned long int reserved; 29 | unsigned long int size; 30 | // requires AspectBlockFlagsHasCopyDisposeHelpers 31 | void (*copy)(void *dst, const void *src); 32 | void (*dispose)(const void *); 33 | // requires AspectBlockFlagsHasSignature 34 | const char *signature; 35 | const char *layout; 36 | } *descriptor; 37 | // imported variables 38 | } *AspectBlockRef; 39 | 40 | @interface AspectInfo : NSObject 41 | - (id)initWithInstance:(__unsafe_unretained id)instance invocation:(NSInvocation *)invocation; 42 | @property (nonatomic, unsafe_unretained, readonly) id instance; 43 | @property (nonatomic, strong, readonly) NSArray *arguments; 44 | @property (nonatomic, strong, readonly) NSInvocation *originalInvocation; 45 | @end 46 | 47 | // Tracks a single aspect. 48 | @interface AspectIdentifier : NSObject 49 | + (instancetype)identifierWithSelector:(SEL)selector object:(id)object options:(AspectOptions)options block:(id)block error:(NSError **)error; 50 | - (BOOL)invokeWithInfo:(id)info; 51 | @property (nonatomic, assign) SEL selector; 52 | @property (nonatomic, strong) id block; 53 | @property (nonatomic, strong) NSMethodSignature *blockSignature; 54 | @property (nonatomic, weak) id object; 55 | @property (nonatomic, assign) AspectOptions options; 56 | @end 57 | 58 | // Tracks all aspects for an object/class. 59 | @interface AspectsContainer : NSObject 60 | - (void)addAspect:(AspectIdentifier *)aspect withOptions:(AspectOptions)injectPosition; 61 | - (BOOL)removeAspect:(id)aspect; 62 | - (BOOL)hasAspects; 63 | @property (atomic, copy) NSArray *beforeAspects; 64 | @property (atomic, copy) NSArray *insteadAspects; 65 | @property (atomic, copy) NSArray *afterAspects; 66 | @end 67 | 68 | @interface AspectTracker : NSObject 69 | - (id)initWithTrackedClass:(Class)trackedClass; 70 | @property (nonatomic, strong) Class trackedClass; 71 | @property (nonatomic, readonly) NSString *trackedClassName; 72 | @property (nonatomic, strong) NSMutableSet *selectorNames; 73 | @property (nonatomic, strong) NSMutableDictionary *selectorNamesToSubclassTrackers; 74 | - (void)addSubclassTracker:(AspectTracker *)subclassTracker hookingSelectorName:(NSString *)selectorName; 75 | - (void)removeSubclassTracker:(AspectTracker *)subclassTracker hookingSelectorName:(NSString *)selectorName; 76 | - (BOOL)subclassHasHookedSelectorName:(NSString *)selectorName; 77 | - (NSSet *)subclassTrackersHookingSelectorName:(NSString *)selectorName; 78 | @end 79 | 80 | @interface NSInvocation (Aspects) 81 | - (NSArray *)aspects_arguments; 82 | @end 83 | 84 | #define AspectPositionFilter 0x07 85 | 86 | #define AspectError(errorCode, errorDescription) do { \ 87 | AspectLogError(@"Aspects: %@", errorDescription); \ 88 | if (error) { *error = [NSError errorWithDomain:AspectErrorDomain code:errorCode userInfo:@{NSLocalizedDescriptionKey: errorDescription}]; }}while(0) 89 | 90 | NSString *const AspectErrorDomain = @"AspectErrorDomain"; 91 | static NSString *const AspectsSubclassSuffix = @"_Aspects_"; 92 | static NSString *const AspectsMessagePrefix = @"aspects_"; 93 | 94 | @implementation NSObject (Aspects) 95 | 96 | /////////////////////////////////////////////////////////////////////////////////////////// 97 | #pragma mark - Public Aspects API 98 | 99 | + (id)aspect_hookSelector:(SEL)selector 100 | withOptions:(AspectOptions)options 101 | usingBlock:(id)block 102 | error:(NSError **)error { 103 | return aspect_add((id)self, selector, options, block, error); 104 | } 105 | 106 | /// @return A token which allows to later deregister the aspect. 107 | - (id)aspect_hookSelector:(SEL)selector 108 | withOptions:(AspectOptions)options 109 | usingBlock:(id)block 110 | error:(NSError **)error { 111 | return aspect_add(self, selector, options, block, error); 112 | } 113 | 114 | /////////////////////////////////////////////////////////////////////////////////////////// 115 | #pragma mark - Private Helper 116 | 117 | static id aspect_add(id self, SEL selector, AspectOptions options, id block, NSError **error) { 118 | NSCParameterAssert(self); 119 | NSCParameterAssert(selector); 120 | NSCParameterAssert(block); 121 | 122 | __block AspectIdentifier *identifier = nil; 123 | aspect_performLocked(^{ 124 | if (aspect_isSelectorAllowedAndTrack(self, selector, options, error)) { 125 | AspectsContainer *aspectContainer = aspect_getContainerForObject(self, selector); 126 | identifier = [AspectIdentifier identifierWithSelector:selector object:self options:options block:block error:error]; 127 | if (identifier) { 128 | [aspectContainer addAspect:identifier withOptions:options]; 129 | 130 | // Modify the class to allow message interception. 131 | aspect_prepareClassAndHookSelector(self, selector, error); 132 | } 133 | } 134 | }); 135 | return identifier; 136 | } 137 | 138 | static BOOL aspect_remove(AspectIdentifier *aspect, NSError **error) { 139 | NSCAssert([aspect isKindOfClass:AspectIdentifier.class], @"Must have correct type."); 140 | 141 | __block BOOL success = NO; 142 | aspect_performLocked(^{ 143 | id self = aspect.object; // strongify 144 | if (self) { 145 | AspectsContainer *aspectContainer = aspect_getContainerForObject(self, aspect.selector); 146 | success = [aspectContainer removeAspect:aspect]; 147 | 148 | aspect_cleanupHookedClassAndSelector(self, aspect.selector); 149 | // destroy token 150 | aspect.object = nil; 151 | aspect.block = nil; 152 | aspect.selector = NULL; 153 | }else { 154 | NSString *errrorDesc = [NSString stringWithFormat:@"Unable to deregister hook. Object already deallocated: %@", aspect]; 155 | AspectError(AspectErrorRemoveObjectAlreadyDeallocated, errrorDesc); 156 | } 157 | }); 158 | return success; 159 | } 160 | 161 | static void aspect_performLocked(dispatch_block_t block) { 162 | static OSSpinLock aspect_lock = OS_SPINLOCK_INIT; 163 | OSSpinLockLock(&aspect_lock); 164 | block(); 165 | OSSpinLockUnlock(&aspect_lock); 166 | } 167 | 168 | static SEL aspect_aliasForSelector(SEL selector) { 169 | NSCParameterAssert(selector); 170 | return NSSelectorFromString([AspectsMessagePrefix stringByAppendingFormat:@"_%@", NSStringFromSelector(selector)]); 171 | } 172 | 173 | static NSMethodSignature *aspect_blockMethodSignature(id block, NSError **error) { 174 | AspectBlockRef layout = (__bridge void *)block; 175 | if (!(layout->flags & AspectBlockFlagsHasSignature)) { 176 | NSString *description = [NSString stringWithFormat:@"The block %@ doesn't contain a type signature.", block]; 177 | AspectError(AspectErrorMissingBlockSignature, description); 178 | return nil; 179 | } 180 | void *desc = layout->descriptor; 181 | desc += 2 * sizeof(unsigned long int); 182 | if (layout->flags & AspectBlockFlagsHasCopyDisposeHelpers) { 183 | desc += 2 * sizeof(void *); 184 | } 185 | if (!desc) { 186 | NSString *description = [NSString stringWithFormat:@"The block %@ doesn't has a type signature.", block]; 187 | AspectError(AspectErrorMissingBlockSignature, description); 188 | return nil; 189 | } 190 | const char *signature = (*(const char **)desc); 191 | return [NSMethodSignature signatureWithObjCTypes:signature]; 192 | } 193 | 194 | static BOOL aspect_isCompatibleBlockSignature(NSMethodSignature *blockSignature, id object, SEL selector, NSError **error) { 195 | NSCParameterAssert(blockSignature); 196 | NSCParameterAssert(object); 197 | NSCParameterAssert(selector); 198 | 199 | BOOL signaturesMatch = YES; 200 | NSMethodSignature *methodSignature = [[object class] instanceMethodSignatureForSelector:selector]; 201 | if (blockSignature.numberOfArguments > methodSignature.numberOfArguments) { 202 | signaturesMatch = NO; 203 | }else { 204 | if (blockSignature.numberOfArguments > 1) { 205 | const char *blockType = [blockSignature getArgumentTypeAtIndex:1]; 206 | if (blockType[0] != '@') { 207 | signaturesMatch = NO; 208 | } 209 | } 210 | // Argument 0 is self/block, argument 1 is SEL or id. We start comparing at argument 2. 211 | // The block can have less arguments than the method, that's ok. 212 | if (signaturesMatch) { 213 | for (NSUInteger idx = 2; idx < blockSignature.numberOfArguments; idx++) { 214 | const char *methodType = [methodSignature getArgumentTypeAtIndex:idx]; 215 | const char *blockType = [blockSignature getArgumentTypeAtIndex:idx]; 216 | // Only compare parameter, not the optional type data. 217 | if (!methodType || !blockType || methodType[0] != blockType[0]) { 218 | signaturesMatch = NO; break; 219 | } 220 | } 221 | } 222 | } 223 | 224 | if (!signaturesMatch) { 225 | NSString *description = [NSString stringWithFormat:@"Block signature %@ doesn't match %@.", blockSignature, methodSignature]; 226 | AspectError(AspectErrorIncompatibleBlockSignature, description); 227 | return NO; 228 | } 229 | return YES; 230 | } 231 | 232 | /////////////////////////////////////////////////////////////////////////////////////////// 233 | #pragma mark - Class + Selector Preparation 234 | 235 | static BOOL aspect_isMsgForwardIMP(IMP impl) { 236 | return impl == _objc_msgForward 237 | #if !defined(__arm64__) 238 | || impl == (IMP)_objc_msgForward_stret 239 | #endif 240 | ; 241 | } 242 | 243 | static IMP aspect_getMsgForwardIMP(NSObject *self, SEL selector) { 244 | IMP msgForwardIMP = _objc_msgForward; 245 | #if !defined(__arm64__) 246 | // As an ugly internal runtime implementation detail in the 32bit runtime, we need to determine of the method we hook returns a struct or anything larger than id. 247 | // https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/LowLevelABI/000-Introduction/introduction.html 248 | // https://github.com/ReactiveCocoa/ReactiveCocoa/issues/783 249 | // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf (Section 5.4) 250 | Method method = class_getInstanceMethod(self.class, selector); 251 | const char *encoding = method_getTypeEncoding(method); 252 | BOOL methodReturnsStructValue = encoding[0] == _C_STRUCT_B; 253 | if (methodReturnsStructValue) { 254 | @try { 255 | NSUInteger valueSize = 0; 256 | NSGetSizeAndAlignment(encoding, &valueSize, NULL); 257 | 258 | if (valueSize == 1 || valueSize == 2 || valueSize == 4 || valueSize == 8) { 259 | methodReturnsStructValue = NO; 260 | } 261 | } @catch (__unused NSException *e) {} 262 | } 263 | if (methodReturnsStructValue) { 264 | msgForwardIMP = (IMP)_objc_msgForward_stret; 265 | } 266 | #endif 267 | return msgForwardIMP; 268 | } 269 | 270 | static void aspect_prepareClassAndHookSelector(NSObject *self, SEL selector, NSError **error) { 271 | NSCParameterAssert(selector); 272 | Class klass = aspect_hookClass(self, error); 273 | Method targetMethod = class_getInstanceMethod(klass, selector); 274 | IMP targetMethodIMP = method_getImplementation(targetMethod); 275 | if (!aspect_isMsgForwardIMP(targetMethodIMP)) { 276 | // Make a method alias for the existing method implementation, it not already copied. 277 | const char *typeEncoding = method_getTypeEncoding(targetMethod); 278 | SEL aliasSelector = aspect_aliasForSelector(selector); 279 | if (![klass instancesRespondToSelector:aliasSelector]) { 280 | __unused BOOL addedAlias = class_addMethod(klass, aliasSelector, method_getImplementation(targetMethod), typeEncoding); 281 | NSCAssert(addedAlias, @"Original implementation for %@ is already copied to %@ on %@", NSStringFromSelector(selector), NSStringFromSelector(aliasSelector), klass); 282 | } 283 | 284 | // We use forwardInvocation to hook in. 285 | class_replaceMethod(klass, selector, aspect_getMsgForwardIMP(self, selector), typeEncoding); 286 | AspectLog(@"Aspects: Installed hook for -[%@ %@].", klass, NSStringFromSelector(selector)); 287 | } 288 | } 289 | 290 | // Will undo the runtime changes made. 291 | static void aspect_cleanupHookedClassAndSelector(NSObject *self, SEL selector) { 292 | NSCParameterAssert(self); 293 | NSCParameterAssert(selector); 294 | 295 | Class klass = object_getClass(self); 296 | BOOL isMetaClass = class_isMetaClass(klass); 297 | if (isMetaClass) { 298 | klass = (Class)self; 299 | } 300 | 301 | // Check if the method is marked as forwarded and undo that. 302 | Method targetMethod = class_getInstanceMethod(klass, selector); 303 | IMP targetMethodIMP = method_getImplementation(targetMethod); 304 | if (aspect_isMsgForwardIMP(targetMethodIMP)) { 305 | // Restore the original method implementation. 306 | const char *typeEncoding = method_getTypeEncoding(targetMethod); 307 | SEL aliasSelector = aspect_aliasForSelector(selector); 308 | Method originalMethod = class_getInstanceMethod(klass, aliasSelector); 309 | IMP originalIMP = method_getImplementation(originalMethod); 310 | NSCAssert(originalMethod, @"Original implementation for %@ not found %@ on %@", NSStringFromSelector(selector), NSStringFromSelector(aliasSelector), klass); 311 | 312 | class_replaceMethod(klass, selector, originalIMP, typeEncoding); 313 | AspectLog(@"Aspects: Removed hook for -[%@ %@].", klass, NSStringFromSelector(selector)); 314 | } 315 | 316 | // Deregister global tracked selector 317 | aspect_deregisterTrackedSelector(self, selector); 318 | 319 | // Get the aspect container and check if there are any hooks remaining. Clean up if there are not. 320 | AspectsContainer *container = aspect_getContainerForObject(self, selector); 321 | if (!container.hasAspects) { 322 | // Destroy the container 323 | aspect_destroyContainerForObject(self, selector); 324 | 325 | // Figure out how the class was modified to undo the changes. 326 | NSString *className = NSStringFromClass(klass); 327 | if ([className hasSuffix:AspectsSubclassSuffix]) { 328 | Class originalClass = NSClassFromString([className stringByReplacingOccurrencesOfString:AspectsSubclassSuffix withString:@""]); 329 | NSCAssert(originalClass != nil, @"Original class must exist"); 330 | object_setClass(self, originalClass); 331 | AspectLog(@"Aspects: %@ has been restored.", NSStringFromClass(originalClass)); 332 | 333 | // We can only dispose the class pair if we can ensure that no instances exist using our subclass. 334 | // Since we don't globally track this, we can't ensure this - but there's also not much overhead in keeping it around. 335 | //objc_disposeClassPair(object.class); 336 | }else { 337 | // Class is most likely swizzled in place. Undo that. 338 | if (isMetaClass) { 339 | aspect_undoSwizzleClassInPlace((Class)self); 340 | }else if (self.class != klass) { 341 | aspect_undoSwizzleClassInPlace(klass); 342 | } 343 | } 344 | } 345 | } 346 | 347 | /////////////////////////////////////////////////////////////////////////////////////////// 348 | #pragma mark - Hook Class 349 | 350 | static Class aspect_hookClass(NSObject *self, NSError **error) { 351 | NSCParameterAssert(self); 352 | Class statedClass = self.class; 353 | Class baseClass = object_getClass(self); 354 | NSString *className = NSStringFromClass(baseClass); 355 | 356 | // Already subclassed 357 | if ([className hasSuffix:AspectsSubclassSuffix]) { 358 | return baseClass; 359 | 360 | // We swizzle a class object, not a single object. 361 | }else if (class_isMetaClass(baseClass)) { 362 | return aspect_swizzleClassInPlace((Class)self); 363 | // Probably a KVO'ed class. Swizzle in place. Also swizzle meta classes in place. 364 | }else if (statedClass != baseClass) { 365 | return aspect_swizzleClassInPlace(baseClass); 366 | } 367 | 368 | // Default case. Create dynamic subclass. 369 | const char *subclassName = [className stringByAppendingString:AspectsSubclassSuffix].UTF8String; 370 | Class subclass = objc_getClass(subclassName); 371 | 372 | if (subclass == nil) { 373 | subclass = objc_allocateClassPair(baseClass, subclassName, 0); 374 | if (subclass == nil) { 375 | NSString *errrorDesc = [NSString stringWithFormat:@"objc_allocateClassPair failed to allocate class %s.", subclassName]; 376 | AspectError(AspectErrorFailedToAllocateClassPair, errrorDesc); 377 | return nil; 378 | } 379 | 380 | aspect_swizzleForwardInvocation(subclass); 381 | aspect_hookedGetClass(subclass, statedClass); 382 | aspect_hookedGetClass(object_getClass(subclass), statedClass); 383 | objc_registerClassPair(subclass); 384 | } 385 | 386 | object_setClass(self, subclass); 387 | return subclass; 388 | } 389 | 390 | static NSString *const AspectsForwardInvocationSelectorName = @"__aspects_forwardInvocation:"; 391 | static void aspect_swizzleForwardInvocation(Class klass) { 392 | NSCParameterAssert(klass); 393 | // If there is no method, replace will act like class_addMethod. 394 | IMP originalImplementation = class_replaceMethod(klass, @selector(forwardInvocation:), (IMP)__ASPECTS_ARE_BEING_CALLED__, "v@:@"); 395 | if (originalImplementation) { 396 | class_addMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName), originalImplementation, "v@:@"); 397 | } 398 | AspectLog(@"Aspects: %@ is now aspect aware.", NSStringFromClass(klass)); 399 | } 400 | 401 | static void aspect_undoSwizzleForwardInvocation(Class klass) { 402 | NSCParameterAssert(klass); 403 | Method originalMethod = class_getInstanceMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName)); 404 | Method objectMethod = class_getInstanceMethod(NSObject.class, @selector(forwardInvocation:)); 405 | // There is no class_removeMethod, so the best we can do is to retore the original implementation, or use a dummy. 406 | IMP originalImplementation = method_getImplementation(originalMethod ?: objectMethod); 407 | class_replaceMethod(klass, @selector(forwardInvocation:), originalImplementation, "v@:@"); 408 | 409 | AspectLog(@"Aspects: %@ has been restored.", NSStringFromClass(klass)); 410 | } 411 | 412 | static void aspect_hookedGetClass(Class class, Class statedClass) { 413 | NSCParameterAssert(class); 414 | NSCParameterAssert(statedClass); 415 | Method method = class_getInstanceMethod(class, @selector(class)); 416 | IMP newIMP = imp_implementationWithBlock(^(id self) { 417 | return statedClass; 418 | }); 419 | class_replaceMethod(class, @selector(class), newIMP, method_getTypeEncoding(method)); 420 | } 421 | 422 | /////////////////////////////////////////////////////////////////////////////////////////// 423 | #pragma mark - Swizzle Class In Place 424 | 425 | static void _aspect_modifySwizzledClasses(void (^block)(NSMutableSet *swizzledClasses)) { 426 | static NSMutableSet *swizzledClasses; 427 | static dispatch_once_t pred; 428 | dispatch_once(&pred, ^{ 429 | swizzledClasses = [NSMutableSet new]; 430 | }); 431 | @synchronized(swizzledClasses) { 432 | block(swizzledClasses); 433 | } 434 | } 435 | 436 | static Class aspect_swizzleClassInPlace(Class klass) { 437 | NSCParameterAssert(klass); 438 | NSString *className = NSStringFromClass(klass); 439 | 440 | _aspect_modifySwizzledClasses(^(NSMutableSet *swizzledClasses) { 441 | if (![swizzledClasses containsObject:className]) { 442 | aspect_swizzleForwardInvocation(klass); 443 | [swizzledClasses addObject:className]; 444 | } 445 | }); 446 | return klass; 447 | } 448 | 449 | static void aspect_undoSwizzleClassInPlace(Class klass) { 450 | NSCParameterAssert(klass); 451 | NSString *className = NSStringFromClass(klass); 452 | 453 | _aspect_modifySwizzledClasses(^(NSMutableSet *swizzledClasses) { 454 | if ([swizzledClasses containsObject:className]) { 455 | aspect_undoSwizzleForwardInvocation(klass); 456 | [swizzledClasses removeObject:className]; 457 | } 458 | }); 459 | } 460 | 461 | /////////////////////////////////////////////////////////////////////////////////////////// 462 | #pragma mark - Aspect Invoke Point 463 | 464 | // This is a macro so we get a cleaner stack trace. 465 | #define aspect_invoke(aspects, info) \ 466 | for (AspectIdentifier *aspect in aspects) {\ 467 | [aspect invokeWithInfo:info];\ 468 | if (aspect.options & AspectOptionAutomaticRemoval) { \ 469 | aspectsToRemove = [aspectsToRemove?:@[] arrayByAddingObject:aspect]; \ 470 | } \ 471 | } 472 | 473 | // This is the swizzled forwardInvocation: method. 474 | static void __ASPECTS_ARE_BEING_CALLED__(__unsafe_unretained NSObject *self, SEL selector, NSInvocation *invocation) { 475 | NSCParameterAssert(self); 476 | NSCParameterAssert(invocation); 477 | SEL originalSelector = invocation.selector; 478 | SEL aliasSelector = aspect_aliasForSelector(invocation.selector); 479 | invocation.selector = aliasSelector; 480 | AspectsContainer *objectContainer = objc_getAssociatedObject(self, aliasSelector); 481 | AspectsContainer *classContainer = aspect_getContainerForClass(object_getClass(self), aliasSelector); 482 | AspectInfo *info = [[AspectInfo alloc] initWithInstance:self invocation:invocation]; 483 | NSArray *aspectsToRemove = nil; 484 | 485 | // Before hooks. 486 | aspect_invoke(classContainer.beforeAspects, info); 487 | aspect_invoke(objectContainer.beforeAspects, info); 488 | 489 | // Instead hooks. 490 | BOOL respondsToAlias = YES; 491 | if (objectContainer.insteadAspects.count || classContainer.insteadAspects.count) { 492 | aspect_invoke(classContainer.insteadAspects, info); 493 | aspect_invoke(objectContainer.insteadAspects, info); 494 | }else { 495 | Class klass = object_getClass(invocation.target); 496 | do { 497 | if ((respondsToAlias = [klass instancesRespondToSelector:aliasSelector])) { 498 | [invocation invoke]; 499 | break; 500 | } 501 | }while (!respondsToAlias && (klass = class_getSuperclass(klass))); 502 | } 503 | 504 | // After hooks. 505 | aspect_invoke(classContainer.afterAspects, info); 506 | aspect_invoke(objectContainer.afterAspects, info); 507 | 508 | // If no hooks are installed, call original implementation (usually to throw an exception) 509 | if (!respondsToAlias) { 510 | invocation.selector = originalSelector; 511 | SEL originalForwardInvocationSEL = NSSelectorFromString(AspectsForwardInvocationSelectorName); 512 | if ([self respondsToSelector:originalForwardInvocationSEL]) { 513 | ((void( *)(id, SEL, NSInvocation *))objc_msgSend)(self, originalForwardInvocationSEL, invocation); 514 | }else { 515 | [self doesNotRecognizeSelector:invocation.selector]; 516 | } 517 | } 518 | 519 | // Remove any hooks that are queued for deregistration. 520 | [aspectsToRemove makeObjectsPerformSelector:@selector(remove)]; 521 | } 522 | #undef aspect_invoke 523 | 524 | /////////////////////////////////////////////////////////////////////////////////////////// 525 | #pragma mark - Aspect Container Management 526 | 527 | // Loads or creates the aspect container. 528 | static AspectsContainer *aspect_getContainerForObject(NSObject *self, SEL selector) { 529 | NSCParameterAssert(self); 530 | SEL aliasSelector = aspect_aliasForSelector(selector); 531 | AspectsContainer *aspectContainer = objc_getAssociatedObject(self, aliasSelector); 532 | if (!aspectContainer) { 533 | aspectContainer = [AspectsContainer new]; 534 | objc_setAssociatedObject(self, aliasSelector, aspectContainer, OBJC_ASSOCIATION_RETAIN); 535 | } 536 | return aspectContainer; 537 | } 538 | 539 | static AspectsContainer *aspect_getContainerForClass(Class klass, SEL selector) { 540 | NSCParameterAssert(klass); 541 | AspectsContainer *classContainer = nil; 542 | do { 543 | classContainer = objc_getAssociatedObject(klass, selector); 544 | if (classContainer.hasAspects) break; 545 | }while ((klass = class_getSuperclass(klass))); 546 | 547 | return classContainer; 548 | } 549 | 550 | static void aspect_destroyContainerForObject(id self, SEL selector) { 551 | NSCParameterAssert(self); 552 | SEL aliasSelector = aspect_aliasForSelector(selector); 553 | objc_setAssociatedObject(self, aliasSelector, nil, OBJC_ASSOCIATION_RETAIN); 554 | } 555 | 556 | /////////////////////////////////////////////////////////////////////////////////////////// 557 | #pragma mark - Selector Blacklist Checking 558 | 559 | static NSMutableDictionary *aspect_getSwizzledClassesDict() { 560 | static NSMutableDictionary *swizzledClassesDict; 561 | static dispatch_once_t pred; 562 | dispatch_once(&pred, ^{ 563 | swizzledClassesDict = [NSMutableDictionary new]; 564 | }); 565 | return swizzledClassesDict; 566 | } 567 | 568 | static BOOL aspect_isSelectorAllowedAndTrack(NSObject *self, SEL selector, AspectOptions options, NSError **error) { 569 | static NSSet *disallowedSelectorList; 570 | static dispatch_once_t pred; 571 | dispatch_once(&pred, ^{ 572 | disallowedSelectorList = [NSSet setWithObjects:@"retain", @"release", @"autorelease", @"forwardInvocation:", nil]; 573 | }); 574 | 575 | // Check against the blacklist. 576 | NSString *selectorName = NSStringFromSelector(selector); 577 | if ([disallowedSelectorList containsObject:selectorName]) { 578 | NSString *errorDescription = [NSString stringWithFormat:@"Selector %@ is blacklisted.", selectorName]; 579 | AspectError(AspectErrorSelectorBlacklisted, errorDescription); 580 | return NO; 581 | } 582 | 583 | // Additional checks. 584 | AspectOptions position = options&AspectPositionFilter; 585 | if ([selectorName isEqualToString:@"dealloc"] && position != AspectPositionBefore) { 586 | NSString *errorDesc = @"AspectPositionBefore is the only valid position when hooking dealloc."; 587 | AspectError(AspectErrorSelectorDeallocPosition, errorDesc); 588 | return NO; 589 | } 590 | 591 | if (![self respondsToSelector:selector] && ![self.class instancesRespondToSelector:selector]) { 592 | NSString *errorDesc = [NSString stringWithFormat:@"Unable to find selector -[%@ %@].", NSStringFromClass(self.class), selectorName]; 593 | AspectError(AspectErrorDoesNotRespondToSelector, errorDesc); 594 | return NO; 595 | } 596 | 597 | // Search for the current class and the class hierarchy IF we are modifying a class object 598 | if (class_isMetaClass(object_getClass(self))) { 599 | Class klass = [self class]; 600 | NSMutableDictionary *swizzledClassesDict = aspect_getSwizzledClassesDict(); 601 | Class currentClass = [self class]; 602 | 603 | AspectTracker *tracker = swizzledClassesDict[currentClass]; 604 | if ([tracker subclassHasHookedSelectorName:selectorName]) { 605 | NSSet *subclassTracker = [tracker subclassTrackersHookingSelectorName:selectorName]; 606 | NSSet *subclassNames = [subclassTracker valueForKey:@"trackedClassName"]; 607 | NSString *errorDescription = [NSString stringWithFormat:@"Error: %@ already hooked subclasses: %@. A method can only be hooked once per class hierarchy.", selectorName, subclassNames]; 608 | AspectError(AspectErrorSelectorAlreadyHookedInClassHierarchy, errorDescription); 609 | return NO; 610 | } 611 | 612 | do { 613 | tracker = swizzledClassesDict[currentClass]; 614 | if ([tracker.selectorNames containsObject:selectorName]) { 615 | if (klass == currentClass) { 616 | // Already modified and topmost! 617 | return YES; 618 | } 619 | NSString *errorDescription = [NSString stringWithFormat:@"Error: %@ already hooked in %@. A method can only be hooked once per class hierarchy.", selectorName, NSStringFromClass(currentClass)]; 620 | AspectError(AspectErrorSelectorAlreadyHookedInClassHierarchy, errorDescription); 621 | return NO; 622 | } 623 | } while ((currentClass = class_getSuperclass(currentClass))); 624 | 625 | // Add the selector as being modified. 626 | currentClass = klass; 627 | AspectTracker *subclassTracker = nil; 628 | do { 629 | tracker = swizzledClassesDict[currentClass]; 630 | if (!tracker) { 631 | tracker = [[AspectTracker alloc] initWithTrackedClass:currentClass]; 632 | swizzledClassesDict[(id)currentClass] = tracker; 633 | } 634 | if (subclassTracker) { 635 | [tracker addSubclassTracker:subclassTracker hookingSelectorName:selectorName]; 636 | } else { 637 | [tracker.selectorNames addObject:selectorName]; 638 | } 639 | 640 | // All superclasses get marked as having a subclass that is modified. 641 | subclassTracker = tracker; 642 | }while ((currentClass = class_getSuperclass(currentClass))); 643 | } else { 644 | return YES; 645 | } 646 | 647 | return YES; 648 | } 649 | 650 | static void aspect_deregisterTrackedSelector(id self, SEL selector) { 651 | if (!class_isMetaClass(object_getClass(self))) return; 652 | 653 | NSMutableDictionary *swizzledClassesDict = aspect_getSwizzledClassesDict(); 654 | NSString *selectorName = NSStringFromSelector(selector); 655 | Class currentClass = [self class]; 656 | AspectTracker *subclassTracker = nil; 657 | do { 658 | AspectTracker *tracker = swizzledClassesDict[currentClass]; 659 | if (subclassTracker) { 660 | [tracker removeSubclassTracker:subclassTracker hookingSelectorName:selectorName]; 661 | } else { 662 | [tracker.selectorNames removeObject:selectorName]; 663 | } 664 | if (tracker.selectorNames.count == 0 && tracker.selectorNamesToSubclassTrackers) { 665 | [swizzledClassesDict removeObjectForKey:currentClass]; 666 | } 667 | subclassTracker = tracker; 668 | }while ((currentClass = class_getSuperclass(currentClass))); 669 | } 670 | 671 | @end 672 | 673 | @implementation AspectTracker 674 | 675 | - (id)initWithTrackedClass:(Class)trackedClass { 676 | if (self = [super init]) { 677 | _trackedClass = trackedClass; 678 | _selectorNames = [NSMutableSet new]; 679 | _selectorNamesToSubclassTrackers = [NSMutableDictionary new]; 680 | } 681 | return self; 682 | } 683 | 684 | - (BOOL)subclassHasHookedSelectorName:(NSString *)selectorName { 685 | return self.selectorNamesToSubclassTrackers[selectorName] != nil; 686 | } 687 | 688 | - (void)addSubclassTracker:(AspectTracker *)subclassTracker hookingSelectorName:(NSString *)selectorName { 689 | NSMutableSet *trackerSet = self.selectorNamesToSubclassTrackers[selectorName]; 690 | if (!trackerSet) { 691 | trackerSet = [NSMutableSet new]; 692 | self.selectorNamesToSubclassTrackers[selectorName] = trackerSet; 693 | } 694 | [trackerSet addObject:subclassTracker]; 695 | } 696 | - (void)removeSubclassTracker:(AspectTracker *)subclassTracker hookingSelectorName:(NSString *)selectorName { 697 | NSMutableSet *trackerSet = self.selectorNamesToSubclassTrackers[selectorName]; 698 | [trackerSet removeObject:subclassTracker]; 699 | if (trackerSet.count == 0) { 700 | [self.selectorNamesToSubclassTrackers removeObjectForKey:selectorName]; 701 | } 702 | } 703 | - (NSSet *)subclassTrackersHookingSelectorName:(NSString *)selectorName { 704 | NSMutableSet *hookingSubclassTrackers = [NSMutableSet new]; 705 | for (AspectTracker *tracker in self.selectorNamesToSubclassTrackers[selectorName]) { 706 | if ([tracker.selectorNames containsObject:selectorName]) { 707 | [hookingSubclassTrackers addObject:tracker]; 708 | } 709 | [hookingSubclassTrackers unionSet:[tracker subclassTrackersHookingSelectorName:selectorName]]; 710 | } 711 | return hookingSubclassTrackers; 712 | } 713 | - (NSString *)trackedClassName { 714 | return NSStringFromClass(self.trackedClass); 715 | } 716 | 717 | - (NSString *)description { 718 | return [NSString stringWithFormat:@"<%@: %@, trackedClass: %@, selectorNames:%@, subclass selector names: %@>", self.class, self, NSStringFromClass(self.trackedClass), self.selectorNames, self.selectorNamesToSubclassTrackers.allKeys]; 719 | } 720 | 721 | @end 722 | 723 | /////////////////////////////////////////////////////////////////////////////////////////// 724 | #pragma mark - NSInvocation (Aspects) 725 | 726 | @implementation NSInvocation (Aspects) 727 | 728 | // Thanks to the ReactiveCocoa team for providing a generic solution for this. 729 | - (id)aspect_argumentAtIndex:(NSUInteger)index { 730 | const char *argType = [self.methodSignature getArgumentTypeAtIndex:index]; 731 | // Skip const type qualifier. 732 | if (argType[0] == _C_CONST) argType++; 733 | 734 | #define WRAP_AND_RETURN(type) do { type val = 0; [self getArgument:&val atIndex:(NSInteger)index]; return @(val); } while (0) 735 | if (strcmp(argType, @encode(id)) == 0 || strcmp(argType, @encode(Class)) == 0) { 736 | __autoreleasing id returnObj; 737 | [self getArgument:&returnObj atIndex:(NSInteger)index]; 738 | return returnObj; 739 | } else if (strcmp(argType, @encode(SEL)) == 0) { 740 | SEL selector = 0; 741 | [self getArgument:&selector atIndex:(NSInteger)index]; 742 | return NSStringFromSelector(selector); 743 | } else if (strcmp(argType, @encode(Class)) == 0) { 744 | __autoreleasing Class theClass = Nil; 745 | [self getArgument:&theClass atIndex:(NSInteger)index]; 746 | return theClass; 747 | // Using this list will box the number with the appropriate constructor, instead of the generic NSValue. 748 | } else if (strcmp(argType, @encode(char)) == 0) { 749 | WRAP_AND_RETURN(char); 750 | } else if (strcmp(argType, @encode(int)) == 0) { 751 | WRAP_AND_RETURN(int); 752 | } else if (strcmp(argType, @encode(short)) == 0) { 753 | WRAP_AND_RETURN(short); 754 | } else if (strcmp(argType, @encode(long)) == 0) { 755 | WRAP_AND_RETURN(long); 756 | } else if (strcmp(argType, @encode(long long)) == 0) { 757 | WRAP_AND_RETURN(long long); 758 | } else if (strcmp(argType, @encode(unsigned char)) == 0) { 759 | WRAP_AND_RETURN(unsigned char); 760 | } else if (strcmp(argType, @encode(unsigned int)) == 0) { 761 | WRAP_AND_RETURN(unsigned int); 762 | } else if (strcmp(argType, @encode(unsigned short)) == 0) { 763 | WRAP_AND_RETURN(unsigned short); 764 | } else if (strcmp(argType, @encode(unsigned long)) == 0) { 765 | WRAP_AND_RETURN(unsigned long); 766 | } else if (strcmp(argType, @encode(unsigned long long)) == 0) { 767 | WRAP_AND_RETURN(unsigned long long); 768 | } else if (strcmp(argType, @encode(float)) == 0) { 769 | WRAP_AND_RETURN(float); 770 | } else if (strcmp(argType, @encode(double)) == 0) { 771 | WRAP_AND_RETURN(double); 772 | } else if (strcmp(argType, @encode(BOOL)) == 0) { 773 | WRAP_AND_RETURN(BOOL); 774 | } else if (strcmp(argType, @encode(bool)) == 0) { 775 | WRAP_AND_RETURN(BOOL); 776 | } else if (strcmp(argType, @encode(char *)) == 0) { 777 | WRAP_AND_RETURN(const char *); 778 | } else if (strcmp(argType, @encode(void (^)(void))) == 0) { 779 | __unsafe_unretained id block = nil; 780 | [self getArgument:&block atIndex:(NSInteger)index]; 781 | return [block copy]; 782 | } else { 783 | NSUInteger valueSize = 0; 784 | NSGetSizeAndAlignment(argType, &valueSize, NULL); 785 | 786 | unsigned char valueBytes[valueSize]; 787 | [self getArgument:valueBytes atIndex:(NSInteger)index]; 788 | 789 | return [NSValue valueWithBytes:valueBytes objCType:argType]; 790 | } 791 | return nil; 792 | #undef WRAP_AND_RETURN 793 | } 794 | 795 | - (NSArray *)aspects_arguments { 796 | NSMutableArray *argumentsArray = [NSMutableArray array]; 797 | for (NSUInteger idx = 2; idx < self.methodSignature.numberOfArguments; idx++) { 798 | [argumentsArray addObject:[self aspect_argumentAtIndex:idx] ?: NSNull.null]; 799 | } 800 | return [argumentsArray copy]; 801 | } 802 | 803 | @end 804 | 805 | /////////////////////////////////////////////////////////////////////////////////////////// 806 | #pragma mark - AspectIdentifier 807 | 808 | @implementation AspectIdentifier 809 | 810 | + (instancetype)identifierWithSelector:(SEL)selector object:(id)object options:(AspectOptions)options block:(id)block error:(NSError **)error { 811 | NSCParameterAssert(block); 812 | NSCParameterAssert(selector); 813 | NSMethodSignature *blockSignature = aspect_blockMethodSignature(block, error); // TODO: check signature compatibility, etc. 814 | if (!aspect_isCompatibleBlockSignature(blockSignature, object, selector, error)) { 815 | return nil; 816 | } 817 | 818 | AspectIdentifier *identifier = nil; 819 | if (blockSignature) { 820 | identifier = [AspectIdentifier new]; 821 | identifier.selector = selector; 822 | identifier.block = block; 823 | identifier.blockSignature = blockSignature; 824 | identifier.options = options; 825 | identifier.object = object; // weak 826 | } 827 | return identifier; 828 | } 829 | 830 | - (BOOL)invokeWithInfo:(id)info { 831 | NSInvocation *blockInvocation = [NSInvocation invocationWithMethodSignature:self.blockSignature]; 832 | NSInvocation *originalInvocation = info.originalInvocation; 833 | NSUInteger numberOfArguments = self.blockSignature.numberOfArguments; 834 | 835 | // Be extra paranoid. We already check that on hook registration. 836 | if (numberOfArguments > originalInvocation.methodSignature.numberOfArguments) { 837 | AspectLogError(@"Block has too many arguments. Not calling %@", info); 838 | return NO; 839 | } 840 | 841 | // The `self` of the block will be the AspectInfo. Optional. 842 | if (numberOfArguments > 1) { 843 | [blockInvocation setArgument:&info atIndex:1]; 844 | } 845 | 846 | void *argBuf = NULL; 847 | for (NSUInteger idx = 2; idx < numberOfArguments; idx++) { 848 | const char *type = [originalInvocation.methodSignature getArgumentTypeAtIndex:idx]; 849 | NSUInteger argSize; 850 | NSGetSizeAndAlignment(type, &argSize, NULL); 851 | 852 | if (!(argBuf = reallocf(argBuf, argSize))) { 853 | AspectLogError(@"Failed to allocate memory for block invocation."); 854 | return NO; 855 | } 856 | 857 | [originalInvocation getArgument:argBuf atIndex:idx]; 858 | [blockInvocation setArgument:argBuf atIndex:idx]; 859 | } 860 | 861 | [blockInvocation invokeWithTarget:self.block]; 862 | 863 | if (argBuf != NULL) { 864 | free(argBuf); 865 | } 866 | return YES; 867 | } 868 | 869 | - (NSString *)description { 870 | return [NSString stringWithFormat:@"<%@: %p, SEL:%@ object:%@ options:%tu block:%@ (#%tu args)>", self.class, self, NSStringFromSelector(self.selector), self.object, self.options, self.block, self.blockSignature.numberOfArguments]; 871 | } 872 | 873 | - (BOOL)remove { 874 | return aspect_remove(self, NULL); 875 | } 876 | 877 | @end 878 | 879 | /////////////////////////////////////////////////////////////////////////////////////////// 880 | #pragma mark - AspectsContainer 881 | 882 | @implementation AspectsContainer 883 | 884 | - (BOOL)hasAspects { 885 | return self.beforeAspects.count > 0 || self.insteadAspects.count > 0 || self.afterAspects.count > 0; 886 | } 887 | 888 | - (void)addAspect:(AspectIdentifier *)aspect withOptions:(AspectOptions)options { 889 | NSParameterAssert(aspect); 890 | NSUInteger position = options&AspectPositionFilter; 891 | switch (position) { 892 | case AspectPositionBefore: self.beforeAspects = [(self.beforeAspects ?:@[]) arrayByAddingObject:aspect]; break; 893 | case AspectPositionInstead: self.insteadAspects = [(self.insteadAspects?:@[]) arrayByAddingObject:aspect]; break; 894 | case AspectPositionAfter: self.afterAspects = [(self.afterAspects ?:@[]) arrayByAddingObject:aspect]; break; 895 | } 896 | } 897 | 898 | - (BOOL)removeAspect:(id)aspect { 899 | for (NSString *aspectArrayName in @[NSStringFromSelector(@selector(beforeAspects)), 900 | NSStringFromSelector(@selector(insteadAspects)), 901 | NSStringFromSelector(@selector(afterAspects))]) { 902 | NSArray *array = [self valueForKey:aspectArrayName]; 903 | NSUInteger index = [array indexOfObjectIdenticalTo:aspect]; 904 | if (array && index != NSNotFound) { 905 | NSMutableArray *newArray = [NSMutableArray arrayWithArray:array]; 906 | [newArray removeObjectAtIndex:index]; 907 | [self setValue:newArray forKey:aspectArrayName]; 908 | return YES; 909 | } 910 | } 911 | return NO; 912 | } 913 | 914 | - (NSString *)description { 915 | return [NSString stringWithFormat:@"<%@: %p, before:%@, instead:%@, after:%@>", self.class, self, self.beforeAspects, self.insteadAspects, self.afterAspects]; 916 | } 917 | 918 | @end 919 | 920 | /////////////////////////////////////////////////////////////////////////////////////////// 921 | #pragma mark - AspectInfo 922 | 923 | @implementation AspectInfo 924 | 925 | @synthesize arguments = _arguments; 926 | 927 | - (id)initWithInstance:(__unsafe_unretained id)instance invocation:(NSInvocation *)invocation { 928 | NSCParameterAssert(instance); 929 | NSCParameterAssert(invocation); 930 | if (self = [super init]) { 931 | _instance = instance; 932 | _originalInvocation = invocation; 933 | } 934 | return self; 935 | } 936 | 937 | - (NSArray *)arguments { 938 | // Lazily evaluate arguments, boxing is expensive. 939 | if (!_arguments) { 940 | _arguments = self.originalInvocation.aspects_arguments; 941 | } 942 | return _arguments; 943 | } 944 | 945 | @end 946 | -------------------------------------------------------------------------------- /InterceptorDemo/Interceptor/UIViewController+Addition.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+Addition.h 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIViewController (Addition) 12 | 13 | // 拦截器是否有效 14 | @property(nonatomic, assign) BOOL disabledInterceptor; 15 | @property(nonatomic, assign) BOOL isInitTheme; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /InterceptorDemo/Interceptor/UIViewController+Addition.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+Addition.m 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import "UIViewController+Addition.h" 10 | #import 11 | 12 | #define KeyIsInitTheme @"KeyIsInitTheme" 13 | #define KeyDisabledInterceptor @"KeyDisabledInterceptor" 14 | 15 | @implementation UIViewController (Addition) 16 | 17 | #pragma mark - inline property 18 | - (BOOL)isInitTheme { 19 | return objc_getAssociatedObject(self, KeyIsInitTheme); 20 | } 21 | 22 | - (void)setIsInitTheme:(BOOL)isInitTheme { 23 | objc_setAssociatedObject(self, KeyIsInitTheme, @(isInitTheme), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 24 | } 25 | 26 | - (BOOL)disabledInterceptor { 27 | return objc_getAssociatedObject(self, KeyDisabledInterceptor); 28 | } 29 | 30 | - (void)setDisabledInterceptor:(BOOL)disabledInterceptor { 31 | NSNumber *integer = @(disabledInterceptor); 32 | if (integer.intValue == 0) { 33 | objc_setAssociatedObject(self, KeyDisabledInterceptor, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 34 | } else { 35 | objc_setAssociatedObject(self, KeyDisabledInterceptor, integer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 36 | } 37 | } 38 | 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /InterceptorDemo/Interceptor/ViewControllerInterceptor.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewControllerInterceptor.h 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewControllerInterceptor : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /InterceptorDemo/Interceptor/ViewControllerInterceptor.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewControllerInterceptor.m 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import "ViewControllerInterceptor.h" 10 | #import "Aspects.h" 11 | #import 12 | #import "UIViewController+Addition.h" 13 | 14 | 15 | @implementation ViewControllerInterceptor 16 | 17 | // 会在应用启动的时候自动被runtime调用,通过这个方法可以实现代码的注入 18 | + (void)load { 19 | [super load]; 20 | [ViewControllerInterceptor sharedInstance]; 21 | } 22 | 23 | // 单例 24 | + (instancetype)sharedInstance { 25 | static dispatch_once_t onceToken; 26 | static ViewControllerInterceptor *sharedInstance; 27 | dispatch_once(&onceToken, ^{ 28 | sharedInstance = [[ViewControllerInterceptor alloc] init]; 29 | }); 30 | return sharedInstance; 31 | } 32 | 33 | - (instancetype)init { 34 | if ([super init]) { 35 | // 使用 Aspects 进行方法的拦截 36 | // AspectOptions 三种方式选择:在原本方法前执行、在原本方法后执行、替换原本方法 37 | [UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id aspectInfo, BOOL animated){ 38 | UIViewController * vc = [aspectInfo instance]; 39 | if (!vc.disabledInterceptor) { 40 | [self viewWillAppear:animated viewController:vc]; 41 | } 42 | } error:NULL]; 43 | } 44 | return self; 45 | } 46 | 47 | // 通过这种方式可以代替原来框架中的基类,不必每个 ViewController 再去继续原框架的基类 48 | #pragma mark - fake methods 49 | - (void)viewWillAppear:(BOOL)animated viewController:(UIViewController *)viewController 50 | { 51 | // 去做基础业务相关的内容 52 | if (!viewController.isInitTheme) { 53 | [self ThemeDidNeedUpdateStyle]; 54 | viewController.isInitTheme = YES; 55 | } 56 | // 其他操作...... 57 | } 58 | 59 | - (void)ThemeDidNeedUpdateStyle { 60 | NSLog(@"Theme did need update style"); 61 | } 62 | 63 | @end 64 | -------------------------------------------------------------------------------- /InterceptorDemo/OtherViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // OtherViewController.h 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface OtherViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /InterceptorDemo/OtherViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // OtherViewController.m 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import "OtherViewController.h" 10 | 11 | @interface OtherViewController () 12 | 13 | 14 | @end 15 | 16 | @implementation OtherViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | self.view.backgroundColor = [UIColor whiteColor]; 21 | } 22 | 23 | - (void)didReceiveMemoryWarning { 24 | [super didReceiveMemoryWarning]; 25 | // Dispose of any resources that can be recreated. 26 | } 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /InterceptorDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InterceptorDemo 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /InterceptorDemoTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /InterceptorDemoTests/InterceptorDemoTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // InterceptorDemoTests.m 3 | // InterceptorDemoTests 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface InterceptorDemoTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation InterceptorDemoTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | // Use XCTAssert and related functions to verify your tests produce the correct results. 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /InterceptorDemoUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /InterceptorDemoUITests/InterceptorDemoUITests.m: -------------------------------------------------------------------------------- 1 | // 2 | // InterceptorDemoUITests.m 3 | // InterceptorDemoUITests 4 | // 5 | // Created by deng on 16/11/15. 6 | // Copyright © 2016年 dengyonghao. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface InterceptorDemoUITests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation InterceptorDemoUITests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | 22 | // In UI tests it is usually best to stop immediately when a failure occurs. 23 | self.continueAfterFailure = NO; 24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 25 | [[[XCUIApplication alloc] init] launch]; 26 | 27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 28 | } 29 | 30 | - (void)tearDown { 31 | // Put teardown code here. This method is called after the invocation of each test method in the class. 32 | [super tearDown]; 33 | } 34 | 35 | - (void)testExample { 36 | // Use recording to get started writing UI tests. 37 | // Use XCTAssert and related functions to verify your tests produce the correct results. 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # InterceptorDemo 2 | 实现一个 iOS 拦截器来取代继承原框架中的基类 3 | --------------------------------------------------------------------------------