2017-05-02 32 views
0

我想用名称mobilepay在swift 3中实现付款模块到我的应用程序。当我的应用程序中的用户购买了一杯咖啡并通过手机付款购买时,他来到了移动支付应用程序,他在那里购买了一杯咖啡。然后,在它完成它的事情之后,他会返回到我们的应用程序,该应用程序会返回一个在我们的应用程序委托中调用的函数(mobilepay自己要求将其放在应用程序委托中)。 当我们回到我们的应用程序时,试图运行观众说像一个警示,我们基本上得到这个错误“购买成功”:当重新打开应用程序时警报不显示

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") 
     }) 
    } 

我:

MobilePay purchase succeeded: Your have now paid for order with id 123456 and MobilePay transaction id 123456789and 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这是我们的代码不知道如何解决这个问题,似乎警告是在页面正确加载之前调用的?

任何帮助将不胜感激。 在此先感谢塞巴斯蒂安。

+0

有任何截图最上面的控制器开始工作? –

+0

找到最顶级的视图控制器并显示您的警报 –

+0

尝试替换此self.window?.rootViewController?.present(alertController,animated:true,completion:nil)** With ** UIApplication.shared.keyWindow?.rootViewController ?.present(alertController,animated:true,completion:nil) – KKRocks

回答

相关问题