2

我一直在编码整个2天的Objective-C。 我有这样的问题:Objective-C程序接收到的信号:“EXC_BAD_ACCESS”推送通知

//Notification methods 
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken started with %@", deviceToken); 
    const void *devTokenBytes = [deviceToken bytes]; 
    self->registered = YES; 
    //NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken ended with %@", devTokenBytes); 
    [self sendProviderDeviceToken:devTokenBytes]; 
} 

- (void) application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
{ 
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError started with %@", error); 
} 

- (BOOL)sendProviderDeviceToken:(void *)deviceToken 
{ 

    //NSLog(@"sendProviderDeviceToken started with %@", deviceToken); 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://some_url//pushRegistration"]]; 
    NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:deviceToken, @"registrationId", @"06d746d0-e67e-11e0-911d-c42c0322474a", @"authenticationToken", @"apns", @"type", nil]; 

    NSError *theError = NULL; 
    NSData *theData = [[CJSONSerializer serializer] serializeObject:dict error:&theError]; 
    NSData *requestData = [NSData dataWithBytes:[theData bytes] length:[theData length]]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody: requestData]; 

    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; 

    NSLog(@"returnData: %@", returnString); 

    return YES; 
} 

你可能认识从苹果开发中心这个例子。 问题是当我试图在sendProviderDeviceToken方法中引用deviceToken时,我得到了EXC_BAD_ACCESS信号。

我在做什么错?

+0

如果我的回答对您的评论有帮助,请将其标记为已接受,以便其他用户认为它有用。 –

+0

我的回答有帮助吗? –

回答

3

设备令牌可太作为字符串(以下片断是从我住的项目之一),方法的

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
    NSLog(@"My token is: %@", deviceToken); 

    NSString *strDeviceToken = [[NSString alloc]initWithFormat:@"%@",[[[deviceToken description] 
                  stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] 
                 stringByReplacingOccurrencesOfString:@" " 
                 withString:@""]]; 
    NSLog(@"%@",strDeviceToken); 

    [self sendProviderDeviceToken:strDeviceToken]; 

} 

更改签名从这个

- (BOOL)sendProviderDeviceToken:(void *)deviceToken 

- (BOOL)sendProviderDeviceToken:(NSString *)deviceToken 
2

你可以尝试使用NSString吗?

NSString *tokenString = [[[[[deviceToken description] 
    stringByReplacingOccurrencesOfString:@"<"withString:@""] 
    stringByReplacingOccurrencesOfString:@">" withString:@""] 
    stringByReplacingOccurrencesOfString: @" " withString: @""] uppercaseString]; 
+0

由于@Jennis的回答,问题得到解决。而且她使用了NSString。 – oriharel

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

你在字典中设置deviceToken所以它应该是字符串的对象类型。

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

     const unsigned *tokenBytes = [deviceToken bytes]; 
      NSString *deviceUDID = [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])]; 

    [self sendProviderDeviceToken:devTokenBytes]; 

} 

和方法应该是字符串类型:

- (BOOL)sendProviderDeviceToken:(NSString *)deviceToken 

这会为你工作!

谢谢

相关问题