2015-05-26 57 views
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同一類耳?

+0

你究竟在哪裏混合?在'+負載'? – Vive

+0

它在庫門面單例的實例化過程中被調用。 –

回答

1

我認爲SKStoreProductViewController是使用遠程XPC服務。 所以你不能在你的應用程序中處理任何東西。 它可以由其他系統進程處理。

如果您在storeProductViewController.view

recursiveDescription

使用recursiveDescription你可以找到_UIRemoteView

相關問題