Last active
August 10, 2018 00:28
-
-
Save colejd/a2a07d9c34fffd511e0a80a88407cbea to your computer and use it in GitHub Desktop.
Swizzles in a randomized color block in place of the cgColor getter on UIColor, randomizing most colors throughout the app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension UIColor { | |
private static let rzl_swizzleImplementation: Void = { | |
let instance: UIColor = UIColor.red // This is a `UICachedDeviceRGBColor` instance. For some reason you have to use red. | |
let _class: AnyClass! = object_getClass(instance) | |
let originalMethod = class_getInstanceMethod(_class, #selector(getter: cgColor)) | |
let swizzledMethod = class_getInstanceMethod(_class, #selector(rzl_randomCGColor)) | |
if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod { | |
method_exchangeImplementations(originalMethod, swizzledMethod) | |
} | |
}() | |
public static func rzl_swizzle() { | |
_ = self.rzl_swizzleImplementation | |
} | |
dynamic func rzl_randomCGColor() -> CGColor { | |
return CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [ | |
CGFloat(Float(arc4random()) / Float(UINT32_MAX)), | |
CGFloat(Float(arc4random()) / Float(UINT32_MAX)), | |
CGFloat(Float(arc4random()) / Float(UINT32_MAX)), | |
CGFloat(Float(arc4random()) / Float(UINT32_MAX)), | |
])! | |
} | |
} |
I made a version that will replicate the functionality of the better, App-Store-unsafe way in a way that will probably not get you rejected: https://gist.github.com/colejd/e41fc68f8335670999a7859df0d6fa93
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will not get you rejected from the App Store (probably), but is a bad idea (probably).
For a method that will absolutely get you rejected, see the other version at https://gist.github.com/colejd/35521af36e019dd2700aa15214fe2696.
To use, just call
UIColor.rzl_swizzle()
somewhere (AppDelegate, for example). Once is enough.