GithubHelp home page GithubHelp logo

ofanweio / aspect-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ikrisliu/aspect

0.0 0.0 0.0 70 KB

Aspect Oriented Programming in Objective-C and Swift

License: MIT License

Objective-C 57.06% Swift 41.41% Ruby 1.53%

aspect-1's Introduction

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.

aspect-1's People

Contributors

ikrisliu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.