She*_*lam 145 iphone cocoa-touch objective-c nsstring nsdata
我正在实施推送通知.我想将我的APNS令牌保存为字符串.
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken
{
NSString *tokenString = [NSString stringWithUTF8String:[newDeviceToken bytes]]; //[[NSString alloc]initWithData:newDeviceToken encoding:NSUTF8StringEncoding];
NSLog(@"%@", tokenString);
NSLog(@"%@", newDeviceToken);
}
Run Code Online (Sandbox Code Playgroud)
第一行代码打印为null.第二个打印令牌.如何将newDeviceToken作为NSString?
Sas*_*cha 219
如果有人想在Swift中这样做:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""
for i in 0..<deviceToken.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
print("tokenString: \(tokenString)")
}
Run Code Online (Sandbox Code Playgroud)
Swift 3引入了Data
具有值语义的类型.要将其转换deviceToken
为String,您可以执行以下操作:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print(token)
}
Run Code Online (Sandbox Code Playgroud)
Shu*_*ank 153
有人用这帮我.我只是路过
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const unsigned *tokenBytes = [deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
[[MyModel sharedModel] setApnsToken:hexToken];
}
Run Code Online (Sandbox Code Playgroud)
Vla*_*kiy 128
你可以用它
- (NSString *)stringWithDeviceToken:(NSData *)deviceToken {
const char *data = [deviceToken bytes];
NSMutableString *token = [NSMutableString string];
for (NSUInteger i = 0; i < [deviceToken length]; i++) {
[token appendFormat:@"%02.2hhX", data[i]];
}
return [token copy];
}
Run Code Online (Sandbox Code Playgroud)
小智 46
用这个 :
NSString * deviceTokenString = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"The generated device token string is : %@",deviceTokenString);
Run Code Online (Sandbox Code Playgroud)
Ana*_*and 41
对于那些想要在Swift 3和最简单的方法
func extractTokenFromData(deviceToken:Data) -> String {
let token = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
return token.uppercased();
}
Run Code Online (Sandbox Code Playgroud)
Zeb*_*Zeb 15
这是我的解决方案,它在我的应用程序中运行良好:
NSString* newToken = [[[NSString stringWithFormat:@"%@",deviceToken]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
Run Code Online (Sandbox Code Playgroud)
NSData
到NSString
与stringWithFormat
小智 15
在 iOS 13 中,描述将采用不同的格式。请使用以下代码获取设备令牌。
- (NSString *)fetchDeviceToken:(NSData *)deviceToken {
NSUInteger len = deviceToken.length;
if (len == 0) {
return nil;
}
const unsigned char *buffer = deviceToken.bytes;
NSMutableString *hexString = [NSMutableString stringWithCapacity:(len * 2)];
for (int i = 0; i < len; ++i) {
[hexString appendFormat:@"%02x", buffer[i]];
}
return [hexString copy];
}
Run Code Online (Sandbox Code Playgroud)
jqg*_*imo 10
说明%02.2hhx
在高投票答案:
%
:介绍x
转化说明符。02
:转换后的值的最小宽度为2。如果转换后的值的字节数少于字段宽度,则应0
在左侧用填充。.2
:给出x
转换说明符出现的最小位数。hh
:指定x
转换说明符适用于有符号的char或无符号的char参数(该参数将根据整数提升而提升,但是在打印之前,其值应转换为有符号的char或无符号的char)。x
:无符号参数应以“ dddd”形式转换为无符号十六进制格式;使用字母“ abcdef”。精度指定出现的最小位数;如果要转换的值可以用较少的数字表示,则应将其扩展为前导零。默认精度为1。以零的显式精度转换零的结果应为无字符。有关更多详细信息,请参见IEEE printf规范。
根据以上解释,我认为更改%02.2hhx
为%02x
或更好%.2x
。
对于Swift 5,以下方法都是可行的:
deviceToken.map({String(format: "%02x", $0)}).joined()
Run Code Online (Sandbox Code Playgroud)
deviceToken.map({String(format: "%.2x", $0)}).joined()
Run Code Online (Sandbox Code Playgroud)
deviceToken.reduce("", {$0 + String(format: "%02x", $1)})
Run Code Online (Sandbox Code Playgroud)
deviceToken.reduce("", {$0 + String(format: "%.2x", $1)})
Run Code Online (Sandbox Code Playgroud)
测试如下:
let deviceToken = (0..<32).reduce(Data(), {$0 + [$1]})
print(deviceToken.reduce("", {$0 + String(format: "%.2x", $1)}))
// Print content:
// 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
Run Code Online (Sandbox Code Playgroud)
我认为将deviceToken转换为十六进制字节字符串是没有意义的.为什么?您将它发送到您的后端,在那里它将被转换回字节以推送到APNS.所以,使用NSData的方法base64EncodedStringWithOptions
,将其推送到服务器,然后使用反向base64decoded数据:)这是如此容易:)
NSString *tokenString = [tokenData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
Run Code Online (Sandbox Code Playgroud)
在iOS 13中description
会损坏,请使用此功能
let deviceTokenString = deviceToken.map { String(format: "%02x", $0) }.joined()
Run Code Online (Sandbox Code Playgroud)
为了清楚起见,让我们分解并解释每个部分:
映射方法对序列的每个元素进行操作。由于Data是Swift中的字节序列,因此将对deviceToken中的每个字节评估传递的闭包。String(format :)初始化程序使用%02x格式说明符评估数据中的每个字节(由匿名参数$ 0表示),以生成零填充的2位十六进制表示形式的字节/ 8位整数。在收集了由map方法创建的每个字节表示形式之后,joind()将每个元素连接为一个字符串。
PS不使用描述在iOS 12和iOS 13中提供了不同的字符串,并且根据将来的范围是不安全的。开发人员不应该依赖特定格式来描述对象。
// iOS 12
(deviceToken as NSData).description // "<965b251c 6cb1926d e3cb366f dfb16ddd e6b9086a 8a3cac9e 5f857679 376eab7C>"
// iOS 13
(deviceToken as NSData).description // "{length = 32, bytes = 0x965b251c 6cb1926d e3cb366f dfb16ddd ... 5f857679 376eab7c }"
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请阅读This。
令牌作为文本...
let tat = deviceToken.map{ data in String(format: "%02.2hhx", data) }.joined()
Run Code Online (Sandbox Code Playgroud)
或者如果你愿意的话
let tat2 = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
Run Code Online (Sandbox Code Playgroud)
(结果是一样的)
归档时间: |
|
查看次数: |
78273 次 |
最近记录: |