从NSData获取设备令牌给出了例外

时间:2011-09-13 12:35:55

标签: iphone memory-management nsdata

我在NSData中获取Device令牌并将其转换为NSString。它正确打印设备令牌

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


    deviceToken = [[devToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];



    deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];


    NSLog(@"Device Token from NSdata is %@",deviceToken);


 }

我从其他班级传递这个

self.Token = [AppDelegate_iPhone sharedAppDelegate].deviceToken;

但它正在崩溃

 NSLog(@"Device Token from NSdata is %@",deviceToken);

*** -[CFString respondsToSelector:]: message sent to deallocated instance 0x1f9d00

2 个答案:

答案 0 :(得分:3)

您只是将值分配给deviceToken。您需要保留该值,否则将立即释放。

deviceToken = [[deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""] retain];

答案 1 :(得分:0)

以下是我过去的做法:

const void *devTokenBytes = [devToken bytes];
NSString *deviceToken = [[[[devToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""];