我们正在实施Apple的新“Promoted In-App Purchases”系统,允许用户单击Apple App Store中的“购买”按钮以触发购买IAP。
系统要求应用程序实现SKPaymentTransactionObserver
的 paymentQueue:shouldAddStorePayment:forProduct:
委托方法,该方法返回一个布尔值。该文件说,
返回true
以继续在您的应用中进行交易。
返回false
以推迟或取消交易。
如果我们仅返回false
,则用户会看到我们的应用程序出现在前台,那么其他任何事情都不会发生。默认情况下,操作系统不会弹出“购买已取消”之类的消息;我想这将决定权留给了应用程序开发人员。
App Store推荐的IAP购买请求可以随时到达,包括当用户处于不应中断的流程中间时。从此方法返回false
是一个完美的案例。当我这样做时,我想向用户显示一条警告消息,说明 UIAlertController
的问题。
问题是,我没有paymentQueue:shouldAddStorePayment:forProduct:
内部使用的上下文视图控制器。通常,当我想从视图控制器内部显示警报时,我会调用[self presentViewController:alert animated:YES completion:nil];
,但是此委托方法中的self
是SKPaymentTransactionObserver
,而不是视图控制器,因此无法正常工作。
我什至不确定在此委托方法触发时是否有保证的 Activity 视图控制器。就我所知,委托方法可以在applicationDidBecomeActive
事件之前触发。
从false
返回paymentQueue:shouldAddStorePayment:forProduct:
时,显示警报(或UI的任何其他摘要)的最佳方法是什么?
最佳答案
您可以get the application window's root view controller并使用它来呈现警报。
Objective-C
id *rootController = [[[[UIApplication sharedApplication]delegate] window] rootViewController];
[rootViewController presentViewController:alertController animated:YES completion:nil];
迅捷
let appDelegate = UIApplication.sharedApplication().delegate
let viewController = appDelegate.window!.rootViewController
viewController.presentViewController(alertController, animated: true, completion: nil)