Created
December 8, 2023 05:10
-
-
Save unixzii/da8c639e55f62b4287ce7714edd2c8a3 to your computer and use it in GitHub Desktop.
A drop-in utility to customize input accessory view for `WKWebView`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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