【问题标题】:App crash on creating NSdictionary for Json in IOS在 IOS 中为 Json 创建 NSdictionary 时应用程序崩溃
【发布时间】:2020-07-14 21:51:56
【问题描述】:

我正在开发应用购买应用,我想在该应用中与我的开发者服务器验证购买交易收据以及其他信息,例如设备供应商 ID、软件版本、设备名称和希望购买的产品 ID。

我正在使用 NSDictionary 创建 Json,但是当我尝试添加时它崩溃了

 NSMutableArray *MainOBJ = [NSMutableArray arrayWithObjects:IDdict,deviceData,kMyFeatureIdentifier,jsonObjectString,nil]; 

其中 IDdict 是设备 id 字符串,deviceData 是字典,其中内容设备信息如名称、软件版本和 kMyFeatureIdentifier 是产品 id NSstring 希望购买。 jsonObjectString 是编码的交易回执字符串。

这是我的代码

- (void)verifyReceipt:(SKPaymentTransaction *)transaction {
 
 //TODO
 // currently working on JSON to send to server .
   NSLog(@"In verifyReceipt method");

   jsonObjectString = [self encode:(uint8_t*)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
 // jsonObjectString=@"TESTING";

NSLog(@"Json Object encoded receipt is %@",jsonObjectString);

NSString *IDdict = [[NSString alloc ]initWithString:[UIDevice currentDevice].identifierForVendor.UUIDString]; // Device UDID

NSArray *objects = [NSArray arrayWithObjects:@"NULL",[[UIDevice currentDevice] model],[[UIDevice currentDevice] name],nil];
NSArray *keys = [NSArray arrayWithObjects:@"serial",@"constructor",@"name",nil];
NSDictionary *deviceData = [NSDictionary dictionaryWithObjects:objects forKeys:keys];  // Device information like name , device model , serial number

NSLog(@"Json question dict created");


//TODO: **It crash here**

NSMutableArray *MainOBJ = [NSMutableArray arrayWithObjects:IDdict,deviceData,kMyFeatureIdentifier,jsonObjectString,nil]; // purchased Item ID of previous item
NSMutableArray *MainKeys = [NSMutableArray arrayWithObjects:@"ID",@"device",@"video","@receiptData", nil];
NSMutableDictionary *MainDict = [NSMutableDictionary dictionaryWithObjects:MainOBJ forKeys:MainKeys]; // final string of data

NSLog(@"Json Main dict created");


NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:MainDict options:NSJSONWritingPrettyPrinted error:&error];
NSString *resultAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Purchase product Json string:\n%@", resultAsString);


ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[[NSURL alloc] initWithString:@"http://xyz/dev.php/video/verifyReceipt"]];
[request setPostValue:resultAsString forKey:@"verify"];
[request setDidFinishSelector:@selector(requestDone:)];
[request setTimeOutSeconds:120];
[request setDelegate:self];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDownloadProgressDelegate:self];
request.showAccurateProgress = YES;

我得到了 "NSLog(@"Json question dict created");"崩溃后在我的日志中。 而我预期的json格式是这样的

        {
      "ID" : "E6E95901-006B-4569-8D2B-FA29A0307F80",
     "device" : {
     "name" : "iPad Simulator",
     "constructor" : "iPad Simulator",
     "serial" : "NULL"
       },
     "video" : "com.amm.happyclip.4445Video",
     "receiptData":"DSKLFKSGERPOKFLJGMZEKLEMSERLKEMZTRKGDGFLefklezkgem"
       }

错误截图

任何建议,感谢您的帮助,谢谢

返回字符串的编码方法,我只是将该字符串分配给“jsonObjectString”

- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {

static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t *output = (uint8_t *)data.mutableBytes;

for (NSInteger i = 0; i < length; i += 3) {
    NSInteger value = 0;
    for (NSInteger j = i; j < (i + 3); j++) {
        value <<= 8;
        
        if (j < length) {
            value |= (0xFF & input[j]);
        }
    }
    
    NSInteger index = (i / 3) * 4;
    output[index + 0] =                    table[(value >> 18) & 0x3F];
    output[index + 1] =                    table[(value >> 12) & 0x3F];
    output[index + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
    output[index + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
       
}

return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

【问题讨论】:

  • 什么。是。这。错误。留言!!?
  • 那些大写的变量名非常不可读。
  • @borrrden 我刚刚在日志(lldb)上得到了这个
  • 这看起来像一个断点。你在那条线上放了一个断点吗?也许你可以把你看到的截图放上来。
  • 为了将来的参考,你有一个 EXC_BAD_ACCESS 错误(不要只是这么说,它需要更多的调试)。如果您想进一步缩小范围,您应该启用异常断点。但是,您似乎找到了导致问题的行。

标签: iphone ios nsdictionary


【解决方案1】:

也许 MainOBJ 中的每个对象都是 nil,如果您使用 nil 对象创建字典,它会使您的应用程序崩溃,或者它们已经被释放。

尝试从 MainOBJ 数组中记录对象。

【讨论】:

  • 我从 mainOBJ 记录对象并且所有对象都完美打印没有一个具有空值
猜你喜欢
  • 1970-01-01
  • 2011-01-20
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
相关资源
最近更新 更多