什么编码是在APN的设备令牌? [英] What encoding is the device token for APNs in?

查看:254
本文介绍了什么编码是在APN的设备令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得令牌从应用程序返回的设备:(UIApplication的*)应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData的*)deviceToken方法?因为我不是在PHP非常好,我想为我的用户手动输入令牌到他们的计算机上的程序,将被用来发送通知。但是,我不能得到这个方法的标记。它记录使用细NSLog的,但是当我使用的NSString initWithData :,我总是得到一些神秘的东西。我猜想的编码是错误的?

感谢您的帮助提前!


解决方案

该令牌是一个NSData对象,它指向数据的原始二进制数据,而不是字符串。将其转换为一个字符串的两个最常见的方式将是:


  1. 的base64 EN code将其
    下载 http://projectswithlove.com/projects/NSData_Base64.zip


 的NSString *海峡= [deviceToken base64En codedStringWithoutNewlines]。



  1. 使用的NSData的-description得到一个字符串十六进制转储和清理


 的NSString *海峡= [[deviceToken说明】stringByReplacingOccurrencesOfString:@< withString:@];
海峡= [STR stringByReplacingOccurrencesOfString:@>中withString:@];
海峡= [STR stringByReplacingOccurrencesOfString:@withString:@];


我$ P $#PFER 1,因为后者依赖于内部方式的NSData的-description通话效果,但无论哪种应该工作。

Is it possible to get the Device Token returned from the application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken method? Since I'm not very good at PHP, I'd like for my user to manually enter the token into a program on their computer that is going to be used to send out the notification. But, I can't get the token from this method. It logs fine using NSLog, but when I use NSString initWithData:, I always get some cryptic thing. I suppose the encoding is wrong?

Thanks for your help in advance!

解决方案

The token is an NSData object, which points to a raw binary blob of data, not a string. The two most common ways to convert it to a string would be:

  1. base64 encode it Download http://projectswithlove.com/projects/NSData_Base64.zip

NSString *str = [deviceToken base64EncodedStringWithoutNewlines];

  1. Use NSData's -description to get a string hex dump and clean it up

NSString *str = [[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@">" withString:@""];
str = [str stringByReplacingOccurrencesOfString: @" " withString: @""];

I prefer #1 because the latter is dependent on the internal way that NSData's -description call works, but either should work.

这篇关于什么编码是在APN的设备令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆