-1

I got iOS deviceToken like this

<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>

now , I want delete the blank-space and "<" ">" , to get to 72c7f0e943d336713b827e234337e391a96873210d2eecc4

what can I do for this ?

The other question is

 - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *deviceString = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];

    NSLog(@"%@",deviceString);
}

but the output is "null" , why ??

Rob
  • 26,989
  • 16
  • 82
  • 98
kevn liu
  • 33
  • 1
  • 7
  • 1
    Just look at soultion here : http://stackoverflow.com/questions/1587407/iphone-device-token-nsdata-or-nsstring. Please atleast try SO for few minutes before posting a new question. – Puneet Sharma Jan 13 '14 at 06:50
  • 1
    than you , because my english is very poor, so .. i'll be careful next time – kevn liu Jan 13 '14 at 08:28

7 Answers7

3
 NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
 token           = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
2

You ask why this didn't work:

NSString *deviceString = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];

That's because the initWithData assumes that the NSData contains a UTF8 string. But in your case, the deviceToken is not a UTF8 string; it is binary data.

The rest of your question presumes that you will create the <72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4> string (presumably that you created with stringWithFormat or description methods), and you're asking how to trim the <, >, and remove the spaces. (I think others have answered that question.)

I might suggest a different approach, though. You could simply have a routine to create the hexadecimal string directly, like so:

NSString *deviceString = [self hexadecimalStringForData:deviceToken];

You could then have a hexadecimalStringForData method like so:

- (NSString *)hexadecimalStringForData:(NSData *)data
{
    NSMutableString *hexadecimalString = [[NSMutableString alloc] initWithCapacity:[data length] * 2];
    uint8_t byte;

    for (NSInteger i = 0; i < [data length]; i++)
    {
        [data getBytes:&byte range:NSMakeRange(i, 1)];
        [hexadecimalString appendFormat:@"%02x", byte];
    }

    return hexadecimalString;
}

You certainly can use the description/stringWithFormat approach and then clean up that string, as others have suggested, but the above strikes me as a more direct solution.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

To replace any character/string in any string, you can use :

[yourString stringByReplacingOccurrencesOfString:strToBeReplaced withString:strToBeReplacedWith];

In your case, you can use this:

NSString *  str= @"<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>";    
str = [str stringByReplacingOccurrencesOfString:@"<" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@">" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];

You can use this also (Taken from this question):

NSString *deviceToken = [[webDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
Community
  • 1
  • 1
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
0

Simply use this code

 - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
    {
    NSLog(@"My token is: %@", deviceToken);
    NSString *devToken;
    devToken = [[[[deviceToken description]
                         stringByReplacingOccurrencesOfString:@"<"withString:@""]
                         stringByReplacingOccurrencesOfString:@">" withString:@""]
                         stringByReplacingOccurrencesOfString:@" "withString:@""];
        NSLog(@"%@",devToken);
    }
Bug
  • 2,576
  • 2
  • 21
  • 36
0

You can use below code for this:

NSString *newString1 = [deviceString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *newString2 = [newString1 stringByReplacingOccurrencesOfString:@"<" withString:@""];
NSString *finalString = [newString2 stringByReplacingOccurrencesOfString:@">" withString:@""];
NSlog("%@",finalString);
Ashutosh
  • 2,215
  • 14
  • 27
0

Use this code.

 - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
        {

    NSString *newToken = [deviceToken description];
        newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
        self.deviceToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
        }
Zeeshan
  • 4,194
  • 28
  • 32
0

Write below method in your view controller .m file

-(NSString*)contentsInParenthesis:(NSString *)str
{
    NSString *subString = nil;
    NSRange range1 = [str rangeOfString:@"<"];
    NSRange range2 = [str rangeOfString:@">"];
    if ((range1.length == 1) && (range2.length == 1) && (range2.location > range1.location))
    {
        NSRange range3;
        range3.location = range1.location+1;
        range3.length = (range2.location - range1.location)-1;
        subString = [str substringWithRange:range3];
    }
    return subString;
} 

And use this method like:

NSString *string = @"<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>";
NSString *str=[self contentsInParenthesis:string];

Now, Printing description of str: 72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38