【问题标题】:alert doesnt show when reopening application重新打开应用程序时不显示警报
【发布时间】:2017-09-29 14:35:00
【问题描述】:

我正在尝试在 swift 3 中为我的应用程序实现一个名为 mobilepay 的支付模块。当我的应用程序上的用户购买了一杯咖啡并使用 mobilepay 按购买时,他会来到 mobilepay 应用程序,在那里他购买了这杯咖啡。然后他在完成它的事情后返回到我们的应用程序,该应用程序返回一个在我们的应用程序委托中调用的函数(mobilepay 自己要求将其放置在应用程序委托中)。 当我们回到我们的应用程序时,我们基本上会在尝试为查看者运行“购买成功”之类的警报时遇到此错误:

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

我不知道如何解决这个问题,似乎在页面正确加载之前调用了警报?

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

【问题讨论】:

  • 有截图吗?
  • 找到最顶部的 View 控制器并显示您的警报
  • 尝试替换这个 self.window?.rootViewController?.present(alertController, animated: true, completion: nil) With UIApplication.shared.keyWindow?.rootViewController?。呈现(alertController,动画:真,完成:无)
  • @SourLeangChhean 我们没有任何屏幕截图,但是您想要什么屏幕截图?我们正在实际的 iphone 而不是模拟器上测试该应用程序,因为它必须安装 mobilepay。
  • @KKRocks 刚刚尝试过,它没有用,如果我们在真正的预提交处理程序,由于 CA 限制,我们实际上无法添加任何新的栅栏”

标签: ios xcode swift3 swift2 ios10


【解决方案1】:

答案正如 Anbu.Karthik 所说,我通过使用这个 stackoverflow https://stackoverflow.com/a/26667122/4814654 找到最顶层的控制器来让它工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    相关资源
    最近更新 更多