У меня проблемы с swizzling viewWillAppear или viewDidAppear на SKStoreProductViewController. Мне нужно знать, когда подкласс этого будет представлен третьей стороной lib.Swizzling SKStoreProductViewController viewWillAppear или viewDidAppear не работает
код я использую:
- (void)swizzleFunnyStoreProductControllerAppear {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originalSelector = @selector(viewWillAppear:);
SEL swizzledSelector = @selector(skViewDidAppearNotification:);
NSString *viewControllerClassString = @"SKStoreProductViewController";
Method originalMethod = class_getInstanceMethod(NSClassFromString(viewControllerClassString), originalSelector);
Method swizzledMethod = class_getInstanceMethod([self class], swizzledSelector);
BOOL didAddMethod =
class_addMethod(NSClassFromString(viewControllerClassString),
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(NSClassFromString(viewControllerClassString),
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)skViewDidAppearNotification:(BOOL)animated {
[[NSNotificationCenter defaultCenter] postNotificationName:ALFunnyViewControllerDidAppearNotification object:NSStringFromClass([self class])];
// calling the original method that is under the replaced name.
[self skViewDidAppearNotification:animated];
}
При запуске, я получаю:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FunnySKStoreProductViewControllerSubClass skViewDidAppearNotification:]: unrecognized selector sent to instance 0x144f1b830'
Используя контрольную точку в [собственной skViewDidAp ....] показывает класс в ответ на селекторы:
(lldb) po [self respondsToSelector:@selector(viewWillAppear:)]
true
(lldb) po [self respondsToSelector:@selector(viewDidAppear:)]
true
(lldb) po [self respondsToSelector:@selector(skViewDidAppearNotification:)]
false
(lldb) po [self respondsToSelector:@selector(skViewDidDisappearNotification:)]
true
Я не могу понять, почему didAddMethod == NO для DidAppear, и почему она работает на том же классе для DidDisapp ухо?
Где именно вы плаваете? в '+ load'? – Vive
Он вызывается во время создания одноэлементного фасада библиотеки. –