Код: Выделить всё
// Called this method in the viewDidLoad
func LinkToken(){
let parameters: Parameters = [
"UserId":self.phoneno,
]
AF.request("https://us-central1-projectid.cloudfunctions.net/PlaidLinkTokenGeneration/post", method: .post, parameters: parameters, encoding: JSONEncoding.default)
.validate(contentType: ["application/json"])
.responseJSON{ response in
let statuscode = response.response?.statusCode
switch response.result{
case .success(let JSON):
if(statuscode == 200) {
let response = JSON as! NSDictionary
let TransId = response.object(forKey: "LinkToken")! as! String
print(TransId)
self.linkToken = TransId
self.createLinkHandler()
self.dismiss(animated: false, completion: nil)
UIApplication.shared.endIgnoringInteractionEvents()
}
else {
self.navigationController?.setNavigationBarHidden(false, animated: false)
self.dismiss(animated: false, completion: nil)
UIApplication.shared.endIgnoringInteractionEvents()
let response = JSON as! NSDictionary
}
case .failure(let error):
self.navigationController?.setNavigationBarHidden(false, animated: false)
print("Request Failed with error:\(error)")
self.dismiss(animated: false, completion: nil)
UIApplication.shared.endIgnoringInteractionEvents()
_ = self.storyboard?.instantiateViewController(withIdentifier: "Payments")as! Payments
self.navigationController?.popViewController(animated: true)
}
}
}
private func createLinkHandler() {
let configuration = createLinkTokenConfiguration()
print("configuration",configuration)
let result = Plaid.create(configuration)
switch result {
case .failure(let error):
print("Unable to create Plaid handler due to: \(error)")
case .success(let handler):
self.handler = handler
openLink()
}
}
private func openLink() {
if let handler = handler {
handler.open(presentUsing: .viewController(self))
}
else{
print("Plaid handler is not available. Link flow cannot be started.")
}
}
private func createLinkTokenConfiguration() -> LinkTokenConfiguration {
var linkConfiguration = LinkTokenConfiguration(token: linkToken) { success in
print("public-token: \(success.publicToken) metadata: \(success.metadata)")
}
linkConfiguration.onExit = { exit in
if let error = exit.error {
print("exit with \(error)\n\(exit.metadata)")
} else {
// User exited the flow without an error.
print("exit with \(exit.metadata)")
}
}
linkConfiguration.onEvent = { event in
print("Link Event: \(event.eventName)")
// Here are the link event printing "TRANSISION_VIEW" and "OPEN"
}
return linkConfiguration
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... link-token