DeviceCheck 500 and 400 errors

I don't seem to be able to get DeviceCheck to work. I have the following on my phone:


    DCDevice.current.generateToken { (token, error) in
      if let token = token {
        let base64EncodedToken = token.base64EncodedString()
        // send token to server
      }
    }


On the server I send the payload with device_token unmodified. Half the time I'll just get 500 error from apple, the other half I get 400 with missing or incorrectly formatted device token payload as message.

Replies

Could you show complete code related to your device check ?


Or check what you did against this tutorial.


h ttps://developerinsider.co/devicecheck-api-unique-identifier-for-the-ios-devices/

Okay, figured it out. I was not encoding the string for url's before submitting so server was interpreting some characters wrong. The following fixes it:


    let encodedToken = base64EncodedToken.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!
    let url = URL(string: "http://www.example.com/a/path")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.httpBody = "device_token=\(token)".data(using: .utf8)