iOS Undocumented Methods


Example

There are a variety of undocumented methods on UIColorwhich expose alternate colors or functionality. These can be found in the UIColor private header file. I will document the use of two private methods, styleString() and _systemDestructiveTintColor().

styleString

Since iOS 2.0 there is a private instance method on UIColor called styleString which returns an RGB or RGBA string representation of the color, even for colors like whiteColor outside the RGB space.

Objective-C:

@interface UIColor (Private)

- (NSString *)styleString;

@end

// ...

[[UIColor whiteColor] styleString]; // rgb(255,255,255)
[[UIColor redColor] styleString]; // rgb(255,0,0)
[[UIColor lightTextColor] styleString]; // rgba(255,255,255,0.600000)

In Swift you could use a bridging header to expose the interface. With pure Swift, you will need to create an @objc protocol with the private method, and unsafeBitCast UIColor with the protocol:

@objc protocol  UIColorPrivate {
    func styleString() -> String
}

let white = UIColor.whiteColor()
let red = UIColor.redColor()
let lightTextColor = UIColor.lightTextColor()

let whitePrivate = unsafeBitCast(white, UIColorPrivate.self)
let redPrivate = unsafeBitCast(red, UIColorPrivate.self)
let lightTextColorPrivate = unsafeBitCast(lightTextColor, UIColorPrivate.self)

whitePrivate.styleString() // rgb(255,255,255)
redPrivate.styleString() // rgb(255,0,0)
lightTextColorPrivate.styleString() // rgba(255,255,255,0.600000)

_systemDestructiveTintColor()

There is an undocumented class method on UIColor called _systemDestructiveTintColor which will return the red color used by destructive system buttons:

let red = UIColor.performSelector("_systemDestructiveTintColor").takeUnretainedValue()

It returns an unmanaged object, which you must call .takeUnretainedValue() on, since the color ownership has not been transferred to our own object.

As with any undocumented API, you should take caution when trying to use this method:

if UIColor.respondsToSelector("_systemDestructiveTintColor") {
    if let red = UIColor.performSelector("_systemDestructiveTintColor").takeUnretainedValue() as? UIColor {
        // use the color
    }
}

or by using a protocol:

@objc protocol UIColorPrivateStatic {
    func _systemDestructiveTintColor() -> UIColor
}

let privateClass = UIColor.self as! UIColorPrivateStatic
privateClass._systemDestructiveTintColor() // UIDeviceRGBColorSpace 1 0.231373 0.188235 1