Not sure if I hit a bug in WebKit
or I am doing something horribly wrong, but I can\'t figure out how to use WKScriptMessageHandler
without causing wha
To fix a retain cycle you can use next common solution that is based on NSProxy for any protocols:
@interface WeakProxy: NSProxy
@property (nonatomic, weak) id object;
@end
@implementation WeakProxy
+ (instancetype)weakProxy:(id)object {
return [[WeakProxy alloc] initWithObject:object];
}
- (instancetype)initWithObject:(id)object {
self.object = object;
return self;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [self.object methodSignatureForSelector:selector];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:self.object];
}
@end
And somewhere in your code you can write:
let proxy = (id)[WeakProxy weakProxy:self];
[configuration.userContentController addScriptMessageHandler:proxy name:KLoginResponseHandler];