所以我使用 DeviceCheck 为每个 iOS 设备创建一个唯一的令牌并将其发送到我的服务器。问题是,我收到的令牌似乎比预期的要长得多(200 多个字符)。
这是代码:
@IBAction func sendInfo(_ sender: Any) {
generateToken()
// not actual server
let url = URL(string: "http://myserver.com/test.asp?")
var request = URLRequest(url: url!)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "v1=\(prefix.text!)&v2=\(email.text!)&v3=\(verification)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}
func generateToken(){
if #available(iOS 11.0, *) {
let currentDevice = DCDevice.current
if currentDevice.isSupported{
currentDevice.generateToken(completionHandler: { (data, error) in
DispatchQueue.main.async {
if let tokenData = data?.base64EncodedString() {
self.verification = tokenData
let alert = UIAlertController(title: "Encoded Device ID", message: "\(self.verification)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
self.present(alert, animated: true)
} else {
self.verification = error?.localizedDescription ?? "Something Wrong!!!"
let alert = UIAlertController(title: "Error", message: "\(self.verification)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
})
} else {
self.verification = "Device not supported."
let alert = UIAlertController(title: "Error", message: "\(verification)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
} else {
// Fallback on earlier versions
}
}
它的作用是通过单击发送按钮向服务器发送 2 个文本输入和一个编码令牌。
不知道为什么令牌这么长(或者大小合适)不知道从这里做什么。任何帮助将不胜感激。