Binary Lion Studios

I code for fun and for food.

iPhone convert device token to string

When you are trying to setup push notifications for an iOS application, you will need to send your device token to a provider. The device token is passed to you by the operating system as binary data. If you need to talk to your provider over HTTP, you’ll need to convert the device token to a string.

1
2
3
4
NSString *deviceTokenStr = [[[deviceToken description]
    stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
    stringByReplacingOccurrencesOfString:@" "
    withString:@""];

At this point, deviceTokenStr will contain the device token as a string, so it can be passed to your provider over HTTP. You might consider throwing that snippet into a category if you need to use it in various spots throughout your app.