我正在尝试使用名称为mobilepay的swift 3中的应用程序实现付费模块。当我的应用程序上的用户购买了一杯咖啡并且使用mobilepay购买时,他来到mobilepay应用程序,在那里他买了一杯咖啡。然后他会在它完成它之后返回我们的应用程序,它返回一个在我们的app委托中调用的函数(mobilepay自己要求将它放在app delegate中)。 当我们回到我们的应用程序时,我们基本上会在尝试为查看器运行警报时出现此错误,例如“购买成功”。
MobilePay purchase succeeded: Your have now paid for order with id 123456 and MobilePay transaction id 12345678901234567890 and the amount withdrawn from the card is: 10.0
2017-05-01 09:05:57.797628+0200 Keebin_development_1[262:10533] Warning: Attempt to present <UIAlertController: 0x16ba0e00> on <Keebin_development_1.LoginViewController: 0x15e91d40> whose view is not in the window hierarchy!
2017-05-01 09:05:59.033882+0200 Keebin_development_1[262:10533] [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction
这是appdelegate中的代码:
func alert(message: String, title: String = "") {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
func application(_ app: UIApplication, open url: URL, options: [String: Any]) -> Bool {
handleMobilePayPayment(with: url)
return true
}
func handleMobilePayPayment(with url: URL) {
MobilePayManager.sharedInstance().handleMobilePayPayment(with: url, success: {( mobilePaySuccessfulPayment: MobilePaySuccessfulPayment?) -> Void in
let orderId: String = mobilePaySuccessfulPayment!.orderId
let transactionId: String = mobilePaySuccessfulPayment!.transactionId
let amountWithdrawnFromCard: String = "(mobilePaySuccessfulPayment!.amountWithdrawnFromCard)"
print("MobilePay purchase succeeded: Your have now paid for order with id (orderId) and MobilePay transaction id (transactionId) and the amount withdrawn from the card is: (amountWithdrawnFromCard)")
self.alert(message: "You have now paid with MobilePay. Your MobilePay transactionId is (transactionId)", title: "MobilePay Succeeded")
}, error: {( error: Error?) -> Void in
// let dict: [AnyHashable: Any]? = error?.userInfo
// let errorMessage: String? = (dict?.value(forKey: NSLocalizedFailureReasonErrorKey) as? String)
// print("MobilePay purchase failed: Error code '(Int(error?.code))' and message '(errorMessage)'")
// self.alert(message: errorMessage!, title: "MobilePay Error (error?.code as! Int)")
self.alert(message: error as! String)
//TODO: show an appropriate error message to the user. Check MobilePayManager.h for a complete description of the error codes
//An example of using the MobilePayErrorCode enum
//if (error.code == MobilePayErrorCodeUpdateApp) {
// NSLog(@"You must update your MobilePay app");
//}
}, cancel: {(_ mobilePayCancelledPayment: MobilePayCancelledPayment?) -> Void in
print("MobilePay purchase with order id (mobilePayCancelledPayment?.orderId!) cancelled by user")
self.alert(message: "You cancelled the payment flow from MobilePay, please pick a fruit and try again", title: "MobilePay Canceled")
})
}
我不知道如何解决这个问题,似乎在页面正确加载之前调用了警报?
非常感谢任何帮助。 在此先感谢塞巴斯蒂安。
答案 0 :(得分:0)
答案就像Anbu.Karthik所说的那样,我通过使用此stackoverflow找到最顶层的控制器来实现它https://stackoverflow.com/a/26667122/4814654