好吧,我已经为这个问题做了一个非常黑客的解决方案:
警告:解决方案包含方法 Swizzling 和对象关联。该解决方案能够通过 Apple 审查,但将来可能会中断。
由于SKStoreReviewPresentationWindow
继承UIWindow
自我在 UIWindow 上创建了一个类别,因此每当显示或隐藏窗口时都会发布事件:
@interface MonitorObject:NSObject
@property (nonatomic, weak) UIWindow* owner;
-(id)init:(UIWindow*)owner;
-(void)dealloc;
@end
@interface UIWindow (DismissNotification)
+ (void)load;
@end
#import "UIWindow+DismissNotification.h"
#import <objc/runtime.h>
@implementation MonitorObject
-(id)init:(UIWindow*)owner
{
self = [super init];
self.owner = owner;
[[NSNotificationCenter defaultCenter] postNotificationName:UIWindowDidBecomeVisibleNotification object:self];
return self;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] postNotificationName:UIWindowDidBecomeHiddenNotification object:self];
}
@end
@implementation UIWindow (DismissNotification)
static NSString* monitorObjectKey = @"monitorKey";
static NSString* partialDescForStoreReviewWindow = @"SKStore";
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(setWindowLevel:);
SEL swizzledSelector = @selector(setWindowLevel_startMonitor:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
#pragma mark - Method Swizzling
- (void)setWindowLevel_startMonitor:(int)level{
[self setWindowLevel_startMonitor:level];
if([self.description containsString:partialDescForStoreReviewWindow])
{
MonitorObject *monObj = [[MonitorObject alloc] init:self];
objc_setAssociatedObject(self, &monitorObjectKey, monObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
@end
像这样使用它:
订阅事件:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidBecomeVisibleNotification:)
name:UIWindowDidBecomeVisibleNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidBecomeHiddenNotification:)
name:UIWindowDidBecomeHiddenNotification
object:nil];
当事件被触发时对它们做出反应:
- (void)windowDidBecomeVisibleNotification:(NSNotification *)notification
{
if([notification.object class] == [MonitorObject class])
{
NSLog(@"Review Window shown!");
}
}
- (void)windowDidBecomeHiddenNotification:(NSNotification *)notification
{
if([notification.object class] == [MonitorObject class])
{
NSLog(@"Review Window hidden!");
}
}
您可以在此处查看解决方案的视频