如何将设备令牌从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>
答案 0 :(得分: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];
}