应用程序崩溃在IOS中为Json创建NSdictionary

时间:2013-06-11 08:28:55

标签: iphone ios nsdictionary

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

我正在使用NSDictionary来创建Json,但是当我尝试添加

时它会崩溃
 NSMutableArray *MainOBJ = [NSMutableArray arrayWithObjects:IDdict,deviceData,kMyFeatureIdentifier,jsonObjectString,nil]; 

其中IDdict是设备ID字符串,deviceData是字典,其中名称,软件版本和kMyFeatureIdentifier等内容设备信息是NSstring希望购买的产品ID。和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问题dict创建“);”崩溃之后在我的日志中。 我期望的json格式就像这样

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

错误的屏幕截图 ![在此输入图像说明] [1]

任何建议,帮助表示感谢

返回字符串的编码方法,我只是将该字符串分配给“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];

}

1 个答案:

答案 0 :(得分:0)

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

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