• This repository has been archived on 06/Feb/2021
  • Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Powerful and easy to use Objective-C swizzling API.

JGMethodSwizzler

Β© 2013-2014 Jonas Gessner


An easy to use Objective-C API for swizzling class and instance methods, as well as swizzling instance methods on specific instances only.

Setup

CocoaPods:
Add this to your Podfile:

pod 'JGMethodSwizzler', '2.0.1'

OR:

Add source Files:
1. Add the `JGMethodSwizzler` folder to your Xcode Project.
2. `#import "JGMethodSwizzler.h"`.

Documentation

#####For further examples see the JGMethodSwizzlerTests Xcode project.

JGMethodSwizzler can be used for three basic swizzling types: Swizzling a specific method for all instances of a class, swizzling class methods and swizzling instance methods of specific instances only.

JGMethodSwizzler is completely thread safe and can handle multiple swizzles. Instance-specific swizzling should however not be combined with global swizzling in the same method.

###Swizzling a class method: Swizzling the method +(int)[TestClass test:(int)]

[TestClass swizzleClassMethod:@selector(test:) withReplacement:JGMethodReplacementProviderBlock {
	//return a replacement block
	return JGMethodReplacement(int, const Class *, int arg) {
		//get the original value
		int orig = JGOriginalImplementation(int, arg);
		//return the modified value
		return orig+2;
	};
}];

After this code is run, calling the method will return the modified value until the method is deswizzled.

###Swizzling an instance method across all instances of a class: Swizzling the method -(int)[TestClass test:(int)]

[TestClass swizzleInstanceMethod:@selector(test:) withReplacement:JGMethodReplacementProviderBlock {
	//return a replacement block
	return JGMethodReplacement(int, TestClass *, int arg) {
		//get the original value
		int orig = JGOriginalImplementation(int, arg);
		//return the modified value
		return orig+2;
	};
}];

After this code is run, calling the method will return the modified value until the method is deswizzled.

###Swizzling an instance method for a specific instance: Swizzling the description method on a specific NSObjectinstance:

NSObject *object = [NSObject new];

[object swizzleMethod:@selector(description) withReplacement:JGMethodReplacementProviderBlock {
	return JGMethodReplacement(NSString *, NSObject *) {
		NSString *orig = JGOriginalImplementation(NSString *);
            
		return [orig stringByAppendingString:@" Swizzled!!"];
	};
}];

After this code is run, calling the method will return the modified value until the method is deswizzled.

###Deswizzling

All swizzles can be removed once they've been applied.

deswizzleAll() removes all swizzles.

####Deswizzling global class and instance swizzles

deswizzleGlobal() removes all swizzles that have been applied as global swizzles (not instance specific).

+deswizzleClassMethod:(SEL) deswizzles a specific class method.

+deswizzleInstanceMethod:(SEL) deswizzles a specific instance method.

+deswizzleAllClassMethods deswizzles all swizzled class methods of this class.

+deswizzleAllInstanceMethods deswizzles all swizzled instance methods of this class.

+deswizzleAllMethods deswizzles all swizzled methods of this class.

####Deswizzling Instance specific swizzles

deswizzleInstances() removes all swizzles that have been applied as instance specific swizzles.

-deswizzleMethod:(SEL) deswizzles a specific instance method of this instance.

-deswizzle deswizzles all swizzled instance methods of this instance.

Notes

JGMethodSwizzler works with both ARC and MRC/MRR.

Credits

Created by Jonas Gessner. Β©2013-2014

Thanks to Andrew Richardson for his inspiration and contribution with InstanceHook.

License

Licensed under the MIT license.

More Repositories

1

JGProgressHUD

An elegant and simple progress HUD for iOS and tvOS, compatible with Swift and ObjC.
Objective-C
3,285
star
2

JGActionSheet

Feature-rich action sheet for iOS. This ActionSheet is a replacement for UIActionSheet, with iPad support!
Objective-C
855
star
3

JGScrollableTableViewCell

A simple UITableViewCell subclass with a scrollable content view, exposing an accessory view when scrolled. Inspired by the iOS 7 mail app. Supports iOS 5+
Objective-C
196
star
4

JGDownloadAcceleration

Download acceleration for iOS. Based on NSURLConnection and NSOperation.
Objective-C
140
star
5

JGProgressHUD-SwiftUI

Easily show HUDs with SwiftUI! Lightweight SwiftUI wrapper for JGProgressHUD for iOS, tvOS, Catalyst.
Swift
99
star
6

JGProgressView

UIProgressView subclass with an animated "Indeterminate" setting (OS X Inspired)
Objective-C
52
star
7

ImageReducer

Drag & drop image reducer. Creates 2x and 1x images out of 3x images on the fly.
Objective-C
42
star
8

CCLoader

Developer utility for loading custom plugins into Control Center on iOS 7 and higher.
Objective-C
38
star
9

JGDetailScrubber

UISlider subclass with variable scrubbing speeds. Inspired by the iOS Music app.
Objective-C
35
star
10

Theos-NIC-Templates

Modern templates for NIC.
C
35
star
11

BrightnessFix

Restore Brightness after killing SpringBoard on iOS 6
Logos
8
star
12

Share-Widget-for-Control-Center

An example widget for Control Center. To be used with CCLoader
Objective-C
5
star
13

Theos-Install-Scripts

Useful scripts for theos projects to install packages via USB rather than SSH.
Python
5
star
14

Open-in-ProTube-2

Opens YouTube links directly in ProTube
Objective-C
4
star
15

SCION-WebRTC

Video Calls over SCION with advanced path control
Swift
4
star
16

SevenCenter

iOS 7 style NotificationCenter for iOS 5 & iOS 6
Logos
4
star
17

ETH-Algolab-2019

Solutions for the ETH Algorithms Lab problems for HS 2019
C++
3
star
18

BridgeConnect

Library for importing media using Bridge.
Objective-C
2
star
19

NoCarrier

Remove the carrier logo from the status bar on iOS.
C
2
star
20

Open-in-ProTube

Opens youtube URLs in ProTube app instead of YouTube app.
Objective-C
2
star
21

WebRTC

WebRTC builds for iOS and macOS that actually (hopefully) work...
C++
1
star
22

ascii85

macOS command line tool for ascii85 encoding and decoding
Swift
1
star
23

Flipswitch-Toggles

Additional switches for Flipswitch
C
1
star