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