0
我無法在SKStoreProductViewController上使用swizzling viewWillAppear或viewDidAppear。我需要知道它的一個子類何時由第三方庫提交。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同一類耳?
你究竟在哪裏混合?在'+負載'? – Vive
它在庫門面單例的實例化過程中被調用。 –