iphone - Objective-C程序收到信号: "EXC_BAD_ACCESS" push notifications

标签 iphone objective-c push-notification exc-bad-access apple-push-notifications

我已经编写 Objective-C 代码整整两天了。 我遇到这个问题:

//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 信号。

我做错了什么?

最佳答案

设备 token 也可以用作字符串(下面的代码片段来 self 的一个实时项目),

- (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

关于iphone - Objective-C程序收到信号: "EXC_BAD_ACCESS" push notifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9002937/

相关文章:

iphone - EAGLView 到 UIImage 的时序问题

iphone - 需要帮助解决简单的 cocos2d 错误

objective-c - 如何从 NSArray 获取元素

android - 推送通知到达 ios (apns) 但不是 android (gcm) 设备

iphone - 一个 iOS 应用程序中的多个 Localizable.strings 文件

iPhone 应用程序正常工作,启动时突然崩溃,重新安装修复程序,但为什么?

ios - 动态调整 UITableViewCell 到 UILabel 的高度

ios - 长按 UIButton 上的手势识别器?

swift - 有没有办法去除 FaSTLane 截图中的 Push Notification Pop Up 确认消息?

node.js - 如何使用 Node.js 的 web-push 包向多个订阅者发送通知?