Skip to content

Instantly share code, notes, and snippets.

@colejd
Last active August 10, 2018 00:30
Show Gist options
  • Save colejd/35521af36e019dd2700aa15214fe2696 to your computer and use it in GitHub Desktop.
Save colejd/35521af36e019dd2700aa15214fe2696 to your computer and use it in GitHub Desktop.
Swizzles in a randomized color block in place of the cgColor getter on UIColor, randomizing every color throughout the app. This WILL get you rejected from the App Store.
public extension UIColor {
private struct StaticVars {
// Names of classes to swizzle. Derived from dumped private headers.
static let classesToSwizzle: [String] = [
"UIColor",
"UIDeviceRGBColor",
"NSColor",
"UIDisplayP3Color",
"UIPlaceholderColor",
"UICIColor",
"UIDeviceWhiteColor",
"UICGColor",
"UICachedDeviceRGBColor",
"UICachedDeviceWhiteColor",
"UICachedDevicePatternColor",
]
static let randomColorBlock: @convention(block) (AnyObject?) -> CGColor = { (_: AnyObject?) -> (CGColor) in
return CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [
CGFloat(Float(arc4random()) / Float(UINT32_MAX)), // R
CGFloat(Float(arc4random()) / Float(UINT32_MAX)), // G
CGFloat(Float(arc4random()) / Float(UINT32_MAX)), // B
1.0, // A
])!
}
}
private static let rzl_swizzleImplementation: Void = {
for className in StaticVars.classesToSwizzle {
if let classFromString = NSClassFromString(className),
let originalMethod = class_getInstanceMethod(classFromString.self, #selector(getter: cgColor)) {
method_setImplementation(originalMethod, imp_implementationWithBlock(unsafeBitCast(StaticVars.randomColorBlock, to: AnyObject.self)))
}
}
}()
public static func rzl_swizzle() {
_ = self.rzl_swizzleImplementation
}
}
@colejd
Copy link
Author

colejd commented Aug 10, 2018

I made a new version of this that does the same thing (perhaps even more invasively), and probably won't 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