一文掌握Swift - URLSession接入短信验证码的操作方法

用Swift通过URLSession方式接入短信验证码,还是非常实用的,分享自己在项目开发中接入短信验证码的过程,如下所示:
`import Foundation

var semaphore = DispatchSemaphore (value: 0)

let parameters = "appId=41KYR0EB&appKey=IIWCKKSR7NOQ&phone=1561894**&templateId=1043&variables=1234"
let postData = parameters.data(using: .utf8)

var request = URLRequest(url: URL(string: "https://vip.veesing.com/smsApi/verifyCode")!,timeoutInterval: Double.infinity)
request.addValue("application/x-www-form-urlencoded;charset=utf-8", forHTTPHeaderField: "Content-Type")

request.httpMethod = "POST"
request.httpBody = postData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {

print(String(describing: error))
return

}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}

task.resume()
semaphore.wait()`

以上就是Swift - URLSession接入短信验证码的操作方法,原创不易,希望大家给个一键三连。
Swift - URLSession.swift和文档说明下载

你可能感兴趣的:(http,https,安全)