Latest 1.0.0
Homepage https://github.com/iKrisLiu/Aspect
License MIT
Platforms ios 10.0, osx 10.11, tvos 10.0, watchos 3.0
Authors

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", from: "1.0.0")
]

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'

Usage

Aspect hooks will add a block of code after/before/instead the current selector

NSObject.hookSelector(with: #selector(doesNotRecognizeSelector(_:)), position: .instead, usingBlock: { aspect in
    print("Do something in this block")
} as AspectBlock)

// Hook method "viewDidAppear" of all UIViewController's instances
UIViewController.hookSelector(with: #selector(UIViewController.viewDidAppear(_:)), position: .after, usingBlock: { aspect in
    print("Do something in this block")
} as AspectBlock)

// Hook only viewController's instance "viewDidLoad"
let viewController = UIViewController()
viewController.hookSelector(with: #selector(UIViewController.viewDidLoad), position: .before, usingBlock: { aspect in
    print("Do something in this block")
} as AspectBlock)
[NSURLSession hookSelectorWith:@selector(sessionWithConfiguration:) position:AspectPositionBefore usingBlock:^{
    NSLog(@"Do something in this block")
}];

NSURLSession *session = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];
[session hookSelectorWith:@selector(getAllTasksWithCompletionHandler:) position:AspectPositionAfter usingBlock:^{
    NSLog(@"Do something in this block");
}];

Limitation

macOS

You can hook any selector with any argument type and any argument count without limitation.

iOS/tvOS/watchOS

Since ARM64 varargs routines changed calling conventions, we can only use work around solution to limit method’s argument count with 6, you can change source code to modify the count if needed. For method’s argument type, it doesn’t support these types that are not NSObject:
float, double, CGPoint, CGSize, CGRect, NSRange and so on.

Some help links about these limitations:
Apple Forum
Apple Developer Documentation

Latest podspec

{
    "name": "Aspect",
    "version": "1.0.0",
    "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.0.0"
    },
    "platforms": {
        "ios": "10.0",
        "osx": "10.11",
        "tvos": "10.0",
        "watchos": "3.0"
    },
    "swift_version": "4.2",
    "module_name": "Aspect",
    "source_files": "Aspect/**/*.{h,m,swift}"
}

Pin It on Pinterest

Share This