Latest | 1.3.1 |
---|---|
Homepage | https://github.com/iKrisLiu/Aspect |
License | MIT |
Platforms | ios 10.0, osx 10.12, tvos 10.0, watchos 3.0 |
Authors |
Aspect Oriented Programming in Objective-C and Swift. (For swift, the method must have @objc dynamic
prefix keyword)
Features
- Hook any objective-c instance/class method
- Hook methods with same name in different classes
Installation
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate Aspect into your Xcode project using Carthage, specify it in your Cartfile
:
github "iKrisLiu/Aspect" ~> 1.0
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. To integrate Aspect into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'Aspect', '~> 1.0'
Swift Package Manager
Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler. To integrate Aspect into your Xcode project, specify it in your Package.swift
.
dependencies: [
.package(url: "https://github.com/iKrisLiu/Aspect", from: "1.0.0")
]
Usage
Aspect hooks will add a block of code after/before/instead the current selector
Swift
// Hook method "viewDidAppear" of all UIViewController's instances
UIViewController.hook(#selector(UIViewController.viewDidAppear(_:)), position: .after, usingBlock: { aspect, animated in
print("Do something in this block")
} as @convention(block) (AspectObject, Bool) -> Void)
// Hook only viewController's instance "viewDidLoad"
let viewController = UIViewController()
viewController.hook(#selector(UIViewController.viewDidLoad), position: .before, usingBlock: { aspect in
print("Do something in this block")
} as AspectBlock)
NSObject.hook(#selector(doesNotRecognizeSelector(_:)), position: .instead, usingBlock: { aspect in
print("Do something in this block")
} as AspectBlock)
// Unhook selector
NSObject.unhookSelector(#selector(doesNotRecognizeSelector(_:)))
Objective-C
[NSURLSession hookSelector:@selector(sessionWithConfiguration:) position:AspectPositionBefore usingBlock:^(AspectObject *aspect, NSURLSessionConfiguration *configuration){
NSLog(@"Do something in this block")
}];
NSURLSession *session = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];
[session hookSelector:@selector(getAllTasksWithCompletionHandler:) position:AspectPositionAfter usingBlock:^(AspectObject *aspect){
NSLog(@"Do something in this block");
}];
[NSURLSession unhookSelector:@selector(sessionWithConfiguration:)];
Reference
Thanks for Aspects which developed by @steipete in GitHub. I referred some codes from his repository.
Latest podspec
{ "name": "Aspect", "version": "1.3.1", "license": "MIT", "summary": "Aspect Oriented Programming in Objective-C and Swift", "homepage": "https://github.com/iKrisLiu/Aspect", "authors": { "Kris Liu": "[email protected]" }, "source": { "git": "https://github.com/iKrisLiu/Aspect.git", "tag": "1.3.1" }, "platforms": { "ios": "10.0", "osx": "10.12", "tvos": "10.0", "watchos": "3.0" }, "swift_versions": [ "4.2", "5.0" ], "module_name": "Aspect", "source_files": "Aspect/**/*.{h,m,swift}" }
Wed, 08 May 2019 10:50:05 +0000