Skip to content

Instantly share code, notes, and snippets.

@unixzii
Created December 8, 2023 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixzii/da8c639e55f62b4287ce7714edd2c8a3 to your computer and use it in GitHub Desktop.
Save unixzii/da8c639e55f62b4287ce7714edd2c8a3 to your computer and use it in GitHub Desktop.
A drop-in utility to customize input accessory view for `WKWebView`.
#import <WebKit/WebKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface WKWebView (InputAccessory)
@property (nonatomic, nullable, setter=cy_setCustomInputAccessoryView:) UIView *cy_customInputAccessoryView;
@end
NS_ASSUME_NONNULL_END
#import <objc/runtime.h>
#import <objc/message.h>
#import "WKWebView+InputAccessory.h"
#define PROTECT_SPI_SELECTOR(x) (x)
@implementation WKWebView (InputAccessory)
+ (void)load {
Class WKContentViewClass = objc_getClass("WKContentView");
SEL targetSEL = sel_registerName(PROTECT_SPI_SELECTOR("inputAccessoryViewForWebView"));
Method targetMethod = class_getInstanceMethod(WKContentViewClass, targetSEL);
IMP origIMP = method_getImplementation(targetMethod);
SEL webViewSEL = sel_registerName(PROTECT_SPI_SELECTOR("webView"));
method_setImplementation(targetMethod, imp_implementationWithBlock(^(id self) {
WKWebView *webView = ((id (*)(id, SEL)) objc_msgSend)(self, webViewSEL);
UIView *customInputAccessoryView = webView.cy_customInputAccessoryView;
if (customInputAccessoryView) {
return (id) customInputAccessoryView;
}
return ((id (*)(id, SEL)) origIMP)(self, targetSEL);
}));
}
- (void)cy_setCustomInputAccessoryView:(UIView *)view {
objc_setAssociatedObject(self, @selector(cy_customInputAccessoryView), view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)cy_customInputAccessoryView {
return objc_getAssociatedObject(self, @selector(cy_customInputAccessoryView));
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment