当我的一项应用内交易完成时,无论是因为它已恢复还是因为它是成功购买,Store Kit 都会生成一个警报视图,其中显示一个确认对话框。在当前版本中,它显示“谢谢。您的购买成功。”。
由于购买成功后我的应用程序应该移动到不同的屏幕,因此我想拦截该对话框并且在用户将其关闭之前不进行转换。问题是我似乎无法控制该对话框。有人对如何做有任何想法吗?
谢谢!
不要尝试。当购买完成时,您的付款代表会收到通知 - 使用该机制。这些警报是 AppStore.app 二进制文件的一部分,不会在您的进程中执行,因此您无法触摸它们。
当这些 StoreKit 警报弹出时,您可以使用应用程序变为非活动状态的事实:
购买完成后检查 UIApplication 的 activeState 属性,如果它是“非活动的”,然后延迟移动到不同的屏幕,直到状态再次变为“活动”(监视 UIApplicationDidBecomeActive 通知)。
4.0 之前的固件不支持“activeState”属性,但您仍然可以手动跟踪应用程序状态更改并随时了解其状态。
我已经用 iOS6、iOS7 和 iOS8 测试了这种技术,一切看起来都很好。
- (void) activeShow;
{
UIApplication *app = [UIApplication sharedApplication];
if (app.applicationState == UIApplicationStateActive) {
[self finishActiveShow];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(becomeActiveNotification:)
name:UIApplicationDidBecomeActiveNotification object:nil];
}
}
- (void) finishActiveShow;
{
if (self.beforeShow) {
self.beforeShow();
}
[self.alert show];
if (self.afterShow) {
self.afterShow();
}
}
- (void) becomeActiveNotification:(id) sender;
{
SPASLog(@"UIApplicationDidBecomeActiveNotification: %@", sender);
// From https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Notifications/Articles/NotificationCenters.html
// "In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself."
// So, it seems that we may not get the notification on the main thread.
dispatch_async(dispatch_get_main_queue(), ^{
[self finishActiveShow];
});
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}