Skip to content

Commit 3280db2

Browse files
committed
README: Added basic examples
1 parent 57fd293 commit 3280db2

File tree

2 files changed

+58
-40
lines changed

2 files changed

+58
-40
lines changed

README-legacy.md

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,5 @@
11
## Usage
22

3-
Let's say you want to amend `sayHi` from `TestClass`:
4-
5-
```swift
6-
class TestClass: NSObject {
7-
// Functions need to be marked as `@objc dynamic` or written in Objective-C.
8-
@objc dynamic func sayHi() -> String {
9-
print("Calling sayHi")
10-
return "Hi there 👋"
11-
}
12-
}
13-
14-
let interposer = try Interpose(TestClass.self) {
15-
try $0.prepareHook(
16-
#selector(TestClass.sayHi),
17-
methodSignature: (@convention(c) (AnyObject, Selector) -> String).self,
18-
hookSignature: (@convention(block) (AnyObject) -> String).self) {
19-
store in { `self` in
20-
print("Before Interposing \(`self`)")
21-
let string = store.original(`self`, store.selector) // free to skip
22-
print("After Interposing \(`self`)")
23-
return string + "and Interpose"
24-
}
25-
}
26-
}
27-
28-
// Don't need the hook anymore? Undo is built-in!
29-
interposer.revert()
30-
```
31-
323
Want to hook just a single instance? No problem!
334

345
```swift
@@ -41,15 +12,6 @@ let hook = try testObj.hook(
4112
}
4213
```
4314

44-
Here's what we get when calling `print(TestClass().sayHi())`
45-
```
46-
[Interposer] Swizzled -[TestClass.sayHi] IMP: 0x000000010d9f4430 -> 0x000000010db36020
47-
Before Interposing <InterposeTests.TestClass: 0x7fa0b160c1e0>
48-
Calling sayHi
49-
After Interposing <InterposeTests.TestClass: 0x7fa0b160c1e0>
50-
Hi there 👋 and Interpose
51-
```
52-
5315
## Object Hooking
5416

5517
InterposeKit can hook classes and object. Class hooking is similar to swizzling, but object-based hooking offers a variety of new ways to set hooks. This is achieved via creating a dynamic subclass at runtime.

README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,72 @@ Then add the product to any target that needs it:
5555

5656
### Class Hook: Instance Method
5757

58-
58+
```swift
59+
class MyClass: NSObject {
60+
@objc dynamic func getValue() -> Int {
61+
return 42
62+
}
63+
}
64+
65+
let object = MyClass()
66+
print(object.getValue()) // => 42
67+
68+
let hook = try Interpose.applyHook(
69+
on: MyClass.self,
70+
for: #selector(MyClass.getValue),
71+
methodSignature: (@convention(c) (MyClass, Selector) -> Int).self,
72+
hookSignature: (@convention(block) (MyClass) -> Int).self
73+
) { hook in
74+
return { `self` in
75+
// Retrieve the original result and add 1 to it. This can be skipped.
76+
return hook.original(`self`, hook.selector) + 1
77+
}
78+
}
79+
80+
print(object.getValue()) // => 43
81+
82+
try hook.revert()
83+
print(object.getValue()) // => 42
84+
```
5985

6086
### Class Hook: Class Method
6187

62-
88+
```swift
89+
class MyClass: NSObject {
90+
@objc dynamic class func getStaticValue() -> Int {
91+
return 42
92+
}
93+
}
94+
95+
print(MyClass.getStaticValue()) // => 42
96+
97+
let hook = try Interpose.applyHook(
98+
on: MyClass.self,
99+
for: #selector(MyClass.getStaticValue),
100+
methodKind: .class,
101+
methodSignature: (@convention(c) (MyClass.Type, Selector) -> Int).self,
102+
hookSignature: (@convention(block) (MyClass.Type) -> Int).self
103+
) { hook in
104+
return { `class` in
105+
// Retrieve the original result and add 1 to it. This can be skipped.
106+
return hook.original(`class`, hook.selector) + 1
107+
}
108+
}
109+
110+
print(MyClass.getStaticValue()) // => 43
111+
112+
try hook.revert()
113+
print(MyClass.getStaticValue()) // => 42
114+
```
63115

64116
### Object Hook
65117

66118
67119

120+
### More Examples
121+
122+
You can check out the extensive test suite to see more advanced examples or the example Xcode project to see more real-life examples of tweaking AppKit classes.
123+
68124
## What’s Changed
69125

70126
Compared to the [original implementation](https://github.com/steipete/InterposeKit), this fork introduces several API and internal changes. Here is a summary of key differences with migration hints.

0 commit comments

Comments
 (0)