/Aspects

Aspect Oriented Programming in Objective-C and Swift

Primary LanguageObjective-CMIT LicenseMIT

Aspect

badge-version badge-pms badge-languages badge-platforms

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

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", .upToNextMajor(from: "1.0.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'

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

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 @convention(block) (AspectObject) -> Void)

NSObject.hook(#selector(doesNotRecognizeSelector(_:)), position: .instead, usingBlock: { aspect in
    print("Do something in this block")
    aspect.originalInvocation.invoke()  // Call original selector
} 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.