0

i am trying to implement a paying module to my application in swift 3 with the name mobilepay. When a user on my application has bought a cup of coffee and presses buy with mobilepay, he comes to the mobilepay application, where he buys the cup of coffee. Then he is returned to our application after it does its thing, which comes back with a function that is called in our app delegate (mobilepay themselves asked for this to be placed in app delegate). When we get back to our application we basically get this error when trying run a alert for the viewer saying like "purchase successful.":

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

this our code in 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")
        })
    }

I have no idea how to solve this issue, it seems the alert is called before the page is loaded properly?

Any help would be greatly appreciated. Thanks in advance, Sebastian.

  • Have any screenshot? – Sour LeangChhean May 02 '17 at 06:41
  • find the top most View controller and present your alert – Anbu.Karthik May 02 '17 at 06:48
  • try to replace this self.window?.rootViewController?.present(alertController, animated: true, completion: nil) **With** UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil) – KKRocks May 02 '17 at 06:53
  • @SourLeangChhean we do not have any screen shots, but what do you want a screen shot of? we're testing the application on a actucal iphone not a emulator as it has to have mobilepay installed. – Sebastian Ryberg May 02 '17 at 06:53
  • @KKRocks just tried this, it did not work, we get the error "2017-05-02 09:04:18.061394+0200 Keebin_development_1[236:6480] [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction" – Sebastian Ryberg May 02 '17 at 07:04
  • @Anbu.Karthik how would we go about doing this? finding the top most view controller and present it? Any example would be greatly appreciated? – Sebastian Ryberg May 02 '17 at 07:05

1 Answers1

0

the answer was as Anbu.Karthik said, i got it to work by finding the topmost controller using this stackoverflow https://stackoverflow.com/a/26667122/4814654

Community
  • 1
  • 1