2

私はObjective-Cを2日間コーディングしています。 私はこの問題を持っている: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; 
} 

おそらくAppleのデベロッパーセンターからこの例を認識しています。 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:(NSString *)deviceToken 
にこの

- (BOOL)sendProviderDeviceToken:(void *)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 

これはあなたのために動作します!

ありがとうございました

関連する問題