我得到了像这样的iOS deviceToken
< 72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>
现在,我要删除空格和“<” “>” 中, 到达,得到 的 72c7f0e943d336713b827e234337e391a96873210d2eecc4
我能为此做些什么?
另一个问题是
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *deviceString = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
NSLog(@"%@",deviceString);
}
但输出为“null”,为什么??
答案 0 :(得分:3)
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
答案 1 :(得分:2)
你问为什么这不起作用:
NSString *deviceString = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
这是因为initWithData
假设NSData
包含UTF8字符串。但在你的情况下,deviceToken
不是UTF8字符串;它是二进制数据。
您的问题的其余部分假设您将创建<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>
字符串(可能是您使用stringWithFormat
或description
方法创建的),并且您正在询问如何修剪{ {1}},<
,然后删除空格。 (我认为其他人已回答了这个问题。)
>
然后您可以使用NSString *deviceString = [self hexadecimalStringForData:deviceToken];
方法:
hexadecimalStringForData
您当然可以使用- (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;
}
/ description
方法,然后像其他人所建议的那样清理该字符串,但上述内容让我觉得是更直接的解决方案。
答案 2 :(得分:0)
要替换任何字符串中的任何字符/字符串,您可以使用:
[yourString stringByReplacingOccurrencesOfString:strToBeReplaced withString:strToBeReplacedWith];
在您的情况下,您可以使用此:
NSString * str= @"<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>";
str = [str stringByReplacingOccurrencesOfString:@"<" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@">" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
你也可以使用它(取自this question):
NSString *deviceToken = [[webDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
答案 3 :(得分:0)
只需使用此代码
即可 - (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);
}
答案 4 :(得分:0)
您可以使用以下代码:
NSString *newString1 = [deviceString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *newString2 = [newString1 stringByReplacingOccurrencesOfString:@"<" withString:@""];
NSString *finalString = [newString2 stringByReplacingOccurrencesOfString:@">" withString:@""];
NSlog("%@",finalString);
答案 5 :(得分:0)
使用此代码。
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
self.deviceToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
}
答案 6 :(得分:0)
在视图控制器.m文件中写下以下方法
-(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;
}
并使用以下方法:
NSString *string = @"<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>";
NSString *str=[self contentsInParenthesis:string];
现在,打印描述str: 72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4