Enam Ajmal Enam Ajmal - 4 months ago 18
iOS Question

I want to store json data and use it when require ios

here is the code

NSString *url = @"http://somesite.com/";
NSURL *stringURL = [NSURL URLWithString:url];
NSLog(@"url-------- %@",stringURL);
NSData *urlData = [NSData dataWithContentsOfURL:stringURL];
NSLog(@"data---------%@",urlData);

if (urlData)
{
// JSON successfully downloaded
NSLog(@"yes ");
}
else
{
NSLog(@"not exist");
}


when i try to print NSData i give me null and control goes to else block. correct me please.. i want to store the json data and than use it where it is needed

Answer

You could try changing your code after the NSURL line to the following:

NSError* error = nil;
NSData* data = [NSData dataWithContentsOfURL:stringURL options:NSDataReadingUncached error:&error];
if (error) {
    NSLog(@"%@", [error localizedDescription]);
} else {
    if (urlData)
    {
        // JSON successfully downloaded
        NSLog(@"yes ");
    }
    else
    {
        NSLog(@"not exist");
    }
}