【问题标题】:How to change the device token from NSData to NSString [duplicate]如何将设备令牌从 NSData 更改为 NSString [重复]
【发布时间】:2015-03-07 10:21:44
【问题描述】:

如何将设备令牌从 NSData 更改为 NSString

  -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        //NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken];
        NSString *deviceTokenString= [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
        deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];


        NSLog(@"DeviceToken : %@", deviceToken);
        [[NSUserDefaults standardUserDefaults] setObject:deviceTokenString forKey:@"DeviceToken"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isNotificationsEnabled"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }

下面是我的输出。

2015-01-09 11:33:22.096 SourceSage[3851:384928] DeviceToken : <cb26ce58 cc0f0229 bc1df7a2 68b4cdb5 ab6351c4 56fb4f8c 39958e8e 3f1f741a>
2015-01-09 11:33:22.109 SourceSage[3851:384928] DeviceToken : <cb26ce58 cc0f0229 bc1df7a2 68b4cdb5 ab6351c4 56fb4f8c 39958e8e 3f1f741a>

【问题讨论】:

标签: ios iphone


【解决方案1】:

您正在将NSData 保存为NSUserDefaults,您需要先将NSData 转换为NSString 并将其保存到NSUserDefaults

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [deviceToken description];
    token           = [token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token           = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"DeviceToken : %@", token);
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isNotificationsEnabled"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

【讨论】:

  • 通过使用这个 iam 得到错误 "use of undeclered identifier 'charlieSendData'
  • No iam not getting the same error is shown 2015-01-09 11:50:27.546 SourceSage[3896:388319] DeviceToken :
  • 是的,但同样的错误正在发生
  • @NayeemuddinShaik:而不是打印deviceToken 打印deviceTokenString。你现在说的错误是什么?
  • divice token is null iam getting
猜你喜欢
  • 2012-03-11
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多