Skip to content

Instantly share code, notes, and snippets.

@chbeer
Created September 12, 2020 15:44
Show Gist options
  • Save chbeer/5a4506d6e657c3b3ea3fdfd0391c9028 to your computer and use it in GitHub Desktop.
Save chbeer/5a4506d6e657c3b3ea3fdfd0391c9028 to your computer and use it in GitHub Desktop.
Prevent PDFView from stealing first responder when setting document
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SwizzlePDFDocumentView()
...
}
#import "SwizzleHelper.h"
// found here: https://github.com/defagos/CoconutKit/blob/master/Framework/Sources/Core/HLSRuntime.h
#import "HLSRuntime.h"
#import <UIKit/UIKit.h>
#import <PDFKit/PDFKit.h>
static BOOL (*s_becomeFirstResponder)(id, SEL) = NULL;
static BOOL swizzle_becomeFirstResponder(id self, SEL _cmd)
{
if ([self isKindOfClass:NSClassFromString(@"PDFDocumentView")]) {
return NO;
}
return s_becomeFirstResponder(self, _cmd);
}
void SwizzlePDFDocumentView() {
HLSSwizzleSelector([UIResponder class], @selector(becomeFirstResponder), swizzle_becomeFirstResponder, &s_becomeFirstResponder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment