我无法在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'
在[self 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
我不知道为什么DidAppear的didAddMethod == NO,以及为什么它在DidDisappear的同一个类上起作用?
最佳答案
我认为SKStoreProductViewController
正在使用远程XPC服务。
因此,您无法处理应用程序中的任何内容。
它可能由其他系统进程处理。
如果在recursiveDescription
上使用storeProductViewController.view
您可以找到_UIRemoteView
。