gpt4 book ai didi

ios - AddCardViewController 完成后不会消失

转载 作者:行者123 更新时间:2023-11-29 05:49:47 25 4
gpt4 key购买 nike

我对 iOS 编程还很陌生,我被困在这里的这一小部分。

我基本上尝试在我的 iOS 应用程序中使用 Stripe。用户输入他们希望支付的金额,按“下一步”,然后输入他们的卡详细信息。到目前为止,所有这些都进展顺利 - 我可以显示 AddCardViewController,并输入他们的卡详细信息。

该代码可以一直验证卡 token 并接受付款;这很棒,但问题是 AddCardViewController 之后不会消失。

当我在演示中尝试它时,它工作得很好,但在我的应用程序上它根本不起作用!该应用程序只是卡在 AddCardViewController 上。点击取消不会执行任何操作,也不会显示消息,并且再次提交卡详细信息只会向用户收取双倍费用。

我做错了什么?

有关更多信息,这是我的代码:

StripeClient.shared.sendPostRequest(with: token, amount: amount, description: description) { result in
switch result {
// 1
case .success:
completion(nil)

let alertController = UIAlertController(title: "Congrats",
message: "Your payment was successful!",
preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: { _ in
self.navigationController?.popViewController(animated: true)
})
alertController.addAction(alertAction)
self.present(alertController, animated: true)

self.view.makeToast("Your payment was successful! We will be sending you a receipt to your email shortly.")
// 2
case .failure(let error):
completion(error)
}
}

我提出了自定义发布请求,因为我无法让演示提供的请求正常工作,但我可以 100% 确定代码成功输入 case .success - 我测试过已经这样了。但为了以防万一,这是我的 .sendPostRequest 方法:

func sendPostRequest(with token: STPToken, amount: Double, description: String, completion: @escaping (Result) -> Void) {
//declare parameter as a dictionary which contains string as key and value combination. considering inputs are valid

let params: [String: Any] = [
"stripeToken": token.tokenId,
"amount": amount,
"currency": Constants.defaultCurrency,
"description": description
]

//create the url with URL
let url = URL(string: "<this-is-my-url.com>")! //change the url

//create the session object
let session = URLSession.shared

//now create the URLRequest object using the url object
var request = URLRequest(url: url)
request.httpMethod = "POST" //set http method as POST

do {
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
} catch let error {
print(error.localizedDescription)
}

request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

//create dataTask using the session object to send data to the server
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

guard error == nil else {
return
}

guard let data = data else {
return
}

do {
//create json object from data
//print(data)
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
//print(json)
// handle json...
//print(json["response"]!)

if let responseString = try json["response"] as? String {
if responseString == "SUCCESS" {
completion(Result.success)
} else {
completion(Result.failure(IntParsingError.overflow))
}
}
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}

我真的非常非常感谢您的帮助!

最佳答案

1-你需要

DispatchQueue.main.async {
completion(Result.success)
}

作为 session.dataTask(with: 在后台线程中运行

2-为此运行

self.navigationController?.popViewController(animated: true)

验证 vc 是否嵌入到导航中,并且您使用前一个 vc 的 push 而不是 present 函数来呈现它

编辑:

当你展示时

self.dismiss(animated: true, completion: nil)

关于ios - AddCardViewController 完成后不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55795314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com