0

I am a new comer to iOS dev. Now I am faced with a weird problem. When I was running the following codes on iPhone 5 simulator, everything works fine. However, it doesn't work on my real iPhone 5. Can anybody do me a favour to help me find out the reason? OBJ-C: NSError *error;

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@1 forKey:@"driverId"];
[dictionary setValue:@1 forKey:@"busId"];
[dictionary setValue:@1 forKey:@"lineId"];

NSData *postData = nil;
NSString *requestData = @"";
NSString *urlPath = @"myurl";
if([NSJSONSerialization isValidJSONObject:dictionary]){
    postData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
    NSString *str = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];
    requestData = [requestData stringByAppendingString:str];

    postData = [NSData dataWithBytes:[requestData UTF8String] length:[requestData length]];
}

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:urlPath];

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
NSString *status = [responseDic objectForKey:@"status"];

By contrasting "request" with "nil", I found that request was nil.

The bug was:

2014-07-23 19:13:54.698 ShuttleBusForDriver[2958:60b] nil request 2014-07-23 19:13:54.699 ShuttleBusForDriver[2958:60b] responseDate: (null) 2014-07-23 19:13:54.701 ShuttleBusForDriver[2958:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' * First throw call stack: (0x2fc96ecb 0x3a431ce7 0x2fc96e0d 0x30624967 0xdfa55 0xde785 0x324cca33 0x324cc7f1 0x32658bf3 0x3257646f 0x32576279 0x32576211 0x324c82e5 0x3214431b 0x3213fb3f 0x3213f9d1 0x3213f3e5 0x3213f1f7 0x324cb99f 0x2fc61faf 0x2fc61477 0x2fc5fc67 0x2fbca729 0x2fbca50b 0x34b396d3 0x3252b871 0xdc69c 0xdc6d8 0x3a92fab7) libc++abi.dylib: terminating with uncaught exception of type NSException

DianaDu
  • 43
  • 5
  • Check if there is any error coming from this line: postData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil]; – Claudio Jul 24 '14 at 02:20
  • Hi,Claudio!Actually, I have tried to make only one url request without adding json in it. The same error appeared. Like doing this way:NSString *urlAsString = @"url path"; NSURL *url = [NSURL URLWithString:urlAsString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; NSURLResponse *response = nil; NSError *error = nil; NSLog(@"Firing synchronous url connection........"); NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; – DianaDu Jul 24 '14 at 02:27
  • @Claudio One more thing, I have tried AFNetworking lib. Same problem happened. I guess it may be related to some configurations or settings of the real iPhone. Am I right? – DianaDu Jul 24 '14 at 02:32
  • As Simon said, try to post the symbolicated crash report. Other thing I would recomend: try to use asynchronous request instead of synchronous (Apple recommends that you don't stop/freeze the main thread) – Claudio Jul 24 '14 at 02:41
  • Thanks.I'm still not sure what is "symbolicated crash report". When my app crashed, where can I get such kind of crash report? From output window or else where?@Claudio – DianaDu Jul 24 '14 at 02:45

3 Answers3

1

@JingDu First apologize for I cannot add a comment due to my stackoverflow website issue. A symbolicated crash report is a report symbolicated, it looks like this:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(
 0   CoreFoundation                      0x0284d1e4 __exceptionPreprocess + 180
 1   libobjc.A.dylib                     0x01c7e8e5 objc_exception_throw + 44
 2   CoreFoundation                      0x0284cfbb +[NSException raise:format:] + 139
 3   Foundation                          0x017b7bd2 +[NSJSONSerialization JSONObjectWithData:options:error:] + 67
 4   Test                                0x00003eb8 -[ViewController viewDidLoad] + 136
 5   UIKit                               0x0085033d -[UIViewController loadViewIfRequired] + 696
 6   UIKit                               0x008505d9 -[UIViewController view] + 35
 7   UIKit                               0x00770267 -[UIWindow addRootViewControllerViewIfPossible] + 66
 8   UIKit                               0x007705ef -[UIWindow _setHidden:forced:] + 312
 9   UIKit                               0x0077086b -[UIWindow _orderFrontWithoutMakingKey] + 49
 10  UIKit                               0x0077b3c8 -[UIWindow makeKeyAndVisible] + 65
 11  UIKit                               0x0072bbc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
 12  UIKit                               0x00730667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
 13  UIKit                               0x00744f92 -[UIApplication handleEvent:withNewEvent:] + 3517
 14  UIKit                               0x00745555 -[UIApplication sendEvent:] + 85
 15  UIKit                               0x00732250 _UIApplicationHandleEvent + 683
 16  GraphicsServices                    0x0255cf02 _PurpleEventCallback + 776
 17  GraphicsServices                    0x0255ca0d PurpleEventCallback + 46
 18  CoreFoundation                      0x027c8ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
 19  CoreFoundation                      0x027c89db __CFRunLoopDoSource1 + 523
 20  CoreFoundation                      0x027f368c __CFRunLoopRun + 2156
 21  CoreFoundation                      0x027f29d3 CFRunLoopRunSpecific + 467
 22  CoreFoundation                      0x027f27eb CFRunLoopRunInMode + 123
 23  UIKit                               0x0072fd9c -[UIApplication _run] + 840
 24  UIKit                               0x00731f9b UIApplicationMain + 1225
 25  Test                                0x000026bd main + 141
 26  libdyld.dylib                       0x02d30725 start + 0
 )
libc++abi.dylib: terminating with uncaught exception of type NSException

It is from similar code like yours:

 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' * First throw call stack: (0x2fc96ecb 0x3a431ce7 0x2fc96e0d 0x30624967 0xdfa55 0xde785 0x324cca33 0x324cc7f1 0x32658bf3 0x3257646f 0x32576279 0x32576211 0x324c82e5 0x3214431b 0x3213fb3f 0x3213f9d1 0x3213f3e5 0x3213f1f7 0x324cb99f 0x2fc61faf 0x2fc61477 0x2fc5fc67 0x2fbca729 0x2fbca50b 0x34b396d3 0x3252b871 0xdc69c 0xdc6d8 0x3a92fab7) libc++abi.dylib: terminating with uncaught exception of type NSException
Simon
  • 237
  • 1
  • 7
  • How did you get it? From output window or somewhere else? It seems that it is a good way to trace back and find the cause. – DianaDu Jul 24 '14 at 02:58
  • I have modified my code according to your instruction. Although the program doesn't crash any more, the NSMutableURLRequest is still nil and there is no crash report. I tried to telnet the IP address with port number. All seems fine. Are there any other possible causes? – DianaDu Jul 24 '14 at 04:53
0

@JingDu Your crash is happening because you are trying to decode a nil object to NSDictionary in this piece of code:

NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
NSString *status = [responseDic objectForKey:@"status"];

Sometimes your urlconnection will timeout or server returns an error, which makes the responseData equals to nil. So you have to check if responseData exists or not. Do it like this:

if (responseData)
{
    NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
    NSString *status = [responseDic objectForKey:@"status"];
}

By the way, you should post your symbolicated crash report to help others locate your issues quickly.

Please mark this answer as accepted if it fixes the crash. Thank you.

Simon
  • 237
  • 1
  • 7
0

This is the problem:

NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

This is the solution:

if ([responseData length] > 0){
    NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

Check the length of the data before you pass it to NSJSONSerialization. Passing it nil is a no no.

quellish
  • 21,123
  • 4
  • 76
  • 83
  • Thanks for your answer. I think you've got the similar solution with Simon. I have to make sure the response is not nil. – DianaDu Jul 24 '14 at 03:05
  • @quelish I have followed your instructions to modify my code. The program doesn't crash any more. However, the request is still nil. I tried to ping my destination IP address. Everything works fine. No symbolicated crash report. – DianaDu Jul 24 '14 at 04:33
  • Your app works fine, it's just not getting the response you expect. That is a problem with the service you are talking to, or how you are talking to it. – quellish Jul 24 '14 at 06:17