2

I want to convert a device token string back into the Data format that was originally given to me through this method:

func application(
    _ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
)

In this function I'm getting the String from deviceToken with

// cd414ecaf22687b31b602cec11c7c4d797ead5cfece5fc59321e7ac35e36d4c1
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()

And I save this token for later use in a notification handler SDK that I'm using. The problem is that this SDK wants the Data representation of the token and not the string.

So how can i convert this string back into the Data representation ?

What I've tried so far is

let tokenString = "cd414ecaf22687b31b602cec11c7c4d797ead5cfece5fc59321e7ac35e36d4c1"
let tokenData = Data(tokenString.utf8)

// 63643431346563616632323638376233316236303263656331316337633464373937656164356366656365356663353933323165376163333565333664346331
let decodedDataBackToString = tokenData.map { String(format: "%02.2hhx", $0) }.joined()
oBo
  • 992
  • 2
  • 13
  • 28
  • If *this SDK wants the Data representation* why do you convert it to String at all? – vadian Feb 08 '23 at 13:05
  • Because I don't want to call the SDK right away – oBo Feb 08 '23 at 13:06
  • But why don't you *save this token for later use* as Data? – vadian Feb 08 '23 at 13:09
  • 2
    I'm using React Native and sending the String to RN, so that I later on can call back to a Swift class with the token. I don't think I can send a Data representation back to RN. Do you think its possible to turn the string back to the Data representation ? – oBo Feb 08 '23 at 13:14

0 Answers0