views:

239

answers:

1

I've already managed to get the devicetoken from APNs. It's type of NSData. So i want to write this deviectoken into my mysql db. I've already tried to convert it to a string without luck. That was my way:

 NSString *tokenTMP = [[NSString alloc] initWithData:devToken encoding:NSASCIIStringEncoding];

If i have the deviceToken in a readable format. How do i use the token in php to send a request to the apns server?

thanks a lot!

+1  A: 

I added the following category to NSData

- (NSString*) stringWithHexBytes 
{
   NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:([self length] * 2)];
   const unsigned char *dataBuffer = [self bytes];

   for (int i = 0; i < [self length]; ++i)
   {
       [stringBuffer appendFormat:@"%02X", (unsigned long)dataBuffer[ i ]];
   }

   return [[stringBuffer retain] autorelease];
}

Then I can just call [devToken stringWithHexBytes]; and send that up to my server and store it as text.

Hope that helps.

chris.

PyjamaSam
thanks for the help.If i want run the app, the compiler complains about([self length] * 2) the error"invalid operands to binary * (have 'id' and 'int')"occurs.
rdesign
I've edited your function, now it works! thanks a lot!- (NSString*) stringWithHexBytes:(NSData *)token;Works like charme :)
rdesign