Skip to content

Commit e72e0b2

Browse files
committed
Merge branch 'master' of github.com:steipete/InterposeKit
2 parents 2f4f9ed + 21acd79 commit e72e0b2

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

.github/workflows/jazzy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ jobs:
6060
6161
cp -a docs/. out/.
6262
cd out
63+
echo "interposekit.com " > CNAME
6364
6465
git add -A
6566
git commit -m "Automated deployment to GitHub Pages: ${GITHUB_SHA}" --allow-empty

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
<!--
99
[![codecov](https://codecov.io/gh/steipete/InterposeKit/branch/master/graph/badge.svg)](https://codecov.io/gh/steipete/InterposeKit) -->
1010

11-
InterposeKit is a modern library to swizzle elegantly in Swift. It is fully written in Swift 5.2+ and works on `@objc dynamic` Swift functions or Objective-C instance methods. API documentation available at [interposekit.com](http://interposekit.com/), and some [implementation thoughts on my blog](https://steipete.com/posts/interposekit/).
11+
InterposeKit is a modern library to swizzle elegantly in Swift. It is [well-documented](http://interposekit.com/), [tested](https://github.com/jsteipete/InterposeKit/actions?query=workflow%3ASwiftPM), written in "pure" Swift 5.2 and works on `@objc dynamic` Swift functions or Objective-C instance methods. The Inspiration for InterposeKit was [a race condition in Mac Catalyst](https://steipete.com/posts/mac-catalyst-crash-hunt/), which required tricky swizzling to fix, I also wrote up [implementation thoughts on my blog](https://steipete.com/posts/interposekit/).
1212

1313
Instead of [adding new methods and exchanging implementations](https://nshipster.com/method-swizzling/) based on [`method_exchangeImplementations`](https://developer.apple.com/documentation/objectivec/1418769-method_exchangeimplementations), this library replaces the implementation directly using [`class_replaceMethod`](https://developer.apple.com/documentation/objectivec/1418677-class_replacemethod). This avoids some of [the usual problems with swizzling](https://pspdfkit.com/blog/2019/swizzling-in-swift/).
1414

1515
You can call the original implementation and add code before, instead or after a method call.
1616
This is similar to the [Aspects library](https://github.com/steipete/Aspects), but doesn't yet do dynamic subclassing.
1717

18+
Compare: [Swizzling a property without helper and with InterposeKit](https://gist.github.com/steipete/f955aaa0742021af15add0133d8482b9)
19+
1820
## Usage
1921

2022
Let's say you want to amend `sayHi` from `TestClass`:
@@ -51,7 +53,7 @@ let interposer = try Interpose(TestClass.self) {
5153
interposer.revert()
5254
```
5355

54-
Here's what we get when calling `print(TestClass().sayHi())`
56+
Here's what we get when calling `print(TestClass().sayHi())`
5557
```
5658
[Interposer] Swizzled -[TestClass.sayHi] IMP: 0x000000010d9f4430 -> 0x000000010db36020
5759
Before Interposing <InterposeTests.TestClass: 0x7fa0b160c1e0>
@@ -62,7 +64,7 @@ Hi there 👋 and Interpose
6264

6365
## Key Facts
6466

65-
- Interpose directly modifies the implementaton of a `Method`, which is [better than selector-based swizzling]((https://pspdfkit.com/blog/2019/swizzling-in-swift/)).
67+
- Interpose directly modifies the implementation of a `Method`, which is [better than selector-based swizzling]((https://pspdfkit.com/blog/2019/swizzling-in-swift/)).
6668
- Hooks can easily be undone via calling `revert()`. This also checks and errors if someone else changed stuff in between.
6769
- Pure Swift, no `NSInvocation`, which requires boxing and can be slow.
6870
- No Type checking. If you have a typo or forget a `convention` part, this will crash at runtime.
@@ -94,10 +96,10 @@ try Interpose.whenAvailable(["RTIInput", "SystemSession"]) {
9496
Naming it Interpose was the plan, but then [SR-898](https://bugs.swift.org/browse/SR-898) came. While having a class with the same name as the module works [in most cases](https://forums.swift.org/t/frameworkname-is-not-a-member-type-of-frameworkname-errors-inside-swiftinterface/28962), [this breaks](https://twitter.com/BalestraPatrick/status/1260928023357878273) when you enable build-for-distribution. There's some [discussion](https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482/81) to get that fixed, but this will be more towards end of 2020, if even.
9597

9698
### I want to hook into Swift! You made another ObjC swizzle thingy, why?
97-
UIKit and AppKit won't go away, and the bugs won't go away either. I see this as a rarely-needed instrument to fix system-level issues. There are ways to do some of that in Swift, but that's a separate (and much more difficult!) project.
99+
UIKit and AppKit won't go away, and the bugs won't go away either. I see this as a rarely-needed instrument to fix system-level issues. There are ways to do some of that in Swift, but that's a separate (and much more difficult!) project.
98100

99101
### Can I ship this?
100-
Yes, absolutely. The goal for this one prokect is a simple library that doesn't try to be too smart. I did this in [Aspects](https://github.com/steipete/Aspects) and while I loved this to no end, it's problematic and can cause side-effects with other code that tries to be clever. InterposeKit is boring, so you don't have to worry about conditions like "We added New Relic to our app and now [your thing crashes](https://github.com/steipete/Aspects/issues/21)".
102+
Yes, absolutely. The goal for this one project is a simple library that doesn't try to be too smart. I did this in [Aspects](https://github.com/steipete/Aspects) and while I loved this to no end, it's problematic and can cause side-effects with other code that tries to be clever. InterposeKit is boring, so you don't have to worry about conditions like "We added New Relic to our app and now [your thing crashes](https://github.com/steipete/Aspects/issues/21)".
101103

102104
### It does not do X!
103105
Pull Requests welcome! You might wanna open a draft before to lay out what you plan, I want to keep the feature-set minimal so it stays simple and no-magic.
@@ -122,12 +124,12 @@ Add `github "steipete/InterposeKit"` to your `Cartfile`.
122124
## Improvement Ideas
123125

124126
- Write proposal to allow to [convert the calling convention of existing types](https://twitter.com/steipete/status/1266799174563041282?s=21).
125-
- Use the C block struct to perfom type checking between Method type and C type (I do that in [Aspects library](https://github.com/steipete/Aspects)), it's still a runtime crash but could be at hook time, not when we call it.
127+
- Use the C block struct to perform type checking between Method type and C type (I do that in [Aspects library](https://github.com/steipete/Aspects)), it's still a runtime crash but could be at hook time, not when we call it.
126128
- Add object-based hooking with dynamic subclassing (Aspects again)
127129
- Add [dyld_dynamic_interpose](https://twitter.com/steipete/status/1258482647933870080) to hook pure C functions
128130
- Combine Promise-API for `Interpose.whenAvailable` for better error bubbling.
129131
- Experiment with [Swift function hooking](https://github.com/rodionovd/SWRoute/wiki/Function-hooking-in-Swift)? ⚡️
130-
- Test against Swift Nightly as Chron Jpb
132+
- Test against Swift Nightly as Cron Job
131133
- I'm sure there's more - Pull Requests or [comments](https://twitter.com/steipete) very welcome!
132134

133135
Make this happen:

0 commit comments

Comments
 (0)