IOS DeviceToken从NSData到NSString到NSDictionary

时间:2015-09-02 06:00:28

标签: nsdata nsmutabledictionary devicetoken

我正在尝试将在didRegisterForRemoteNotificationsWithDeviceToken下为IOS接收的deviceToken传递给另一个接受NSDictionary的方法,该方法将作为POST请求(JSON格式)进一步发送到我的后端服务器。

我能够将NSData格式标记转换为NSString但是当我尝试将其转换为NSMutableDictionary时,我得到一个Null。这是我的代码:

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

     NSLog(@"Entered didRegisterForRemoteNotification with token: %@", deviceToken);

     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])];
// Token printed below looks good.
     NSLog(@"token in String format: %@",hexToken);

     NSMutableDictionary* myRegistrationData;
     [myRegistrationData setValue:hexToken forKey:@"deviceToken"];

     NSLog(@"sending token as NSMutableDictionary: %@",myRegistrationData);
            // the Dictionary with token printed here prints Null.

     [apiFromAppDelegate registerTokenWithAppServer:myRegistrationData];

}

提前致谢。

0 个答案:

没有答案