Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Because this is still haunting our customers:
BOOL PSPDFSystemFunctionOverriddenInCategory(Class theClass, SEL selector, NSString *__autoreleasing* categoryName) {
Dl_info info;
if (dladdr(class_getMethodImplementation(theClass, selector), &info)) {
// /System/Library/Frameworks is a common denominator, some methods are in /usr/lib/libobjc.A.dylib
// Custom app is in /private/var/mobile/Containers/Bundle/Application/<UID>/
if (!strstr(info.dli_fname, "/System/Library") && !strstr(info.dli_fname, "/usr/lib")) {
if (categoryName) *categoryName = @(info.dli_sname);
return YES;
return NO;
void PSPDFCheckAgainstIncompatible3rdPartyCode(void) {
// Is - [NSArray firstObject] overridden?
NSString *categoryName;
if (PSPDFSystemFunctionOverriddenInCategory(NSArray.class, @selector(firstObject), &categoryName)) {
// Test our assumption of the faulty category.
@try { [@[] firstObject]; }
@catch (__unused NSException *exception) {
PSPDFLogError(@"The method `firstObject` of NSArray is overridden in the category %@, which performs differently than Apple's default. Check your categories and 3rd-party code and remove/update the culprit. We're repairing that for now.", categoryName);
// Repair it.
PSPDFSwizzleMethodImplementation(NSArray.class, @selector(firstObject), ^id(NSArray *_self) {
return _self.count ? _self[0] : nil;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment