Xcode 7 Json Fetching always crashing

453 views Asked by At

Whenever I am trying to fetch data through JSon in xcode 7 it is giving me error :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

In xcode 6 it is working fine. Is new method has been implemented in xcode 7 to fetch data from json .

[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"]];


id response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
2

There are 2 answers

1
arwaz shaikh On

In iOS 9.0 it will no more support http:// it need https://

adding this to info.plist solve the issue but don't know will apple allow such app to publish in app store .

< key>NSAppTransportSecurity< / key>
     < dict>
        < key>NSAllowsArbitraryLoads< / key>< true/>
     < / dict>

Adding this by editing info.plist in text editor doesn't work , has to do from xcode itself. (I am a new to programming)

2
iSekhar On
NSError *error;
NSURL *url = [NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error:&error];
if (!data)
{
    NSLog(@"Download Error: %@", error.localizedDescription);

}

// Parsing the JSON data received from web service into an NSDictionary object
NSDictionary *jSON =
[NSJSONSerialization JSONObjectWithData: data
                                options: NSJSONReadingMutableContainers
                                  error: &error];

try this its working