gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:25:34 28 4
gpt4 key购买 nike

我已经编写 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com