我是iOS编程的新手,我只停留在这小部分。
我基本上是想在我的iOS应用程序中使用Stripe。用户输入他们想要支付的金额,按NEXT,然后输入卡的详细信息。到目前为止,所有这些都进行得很好-我可以显示AddCardViewController并输入其卡的详细信息。
该代码可以一路验证卡片令牌并接受付款;很棒,但是接下来的问题是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()
}
我真的非常非常感谢您的帮助!
答案 0 :(得分:0)
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)