I am using this code to load data to my App, can you tell me how can I make this asynchronously?
NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request2 delegate:self];
if (connection)
{
NSLog(@"NSURLConnection connection==true");
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&err];
self.news =[NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];
NSLog(@"responseData: %@", self.news);
}
else
{
NSLog(@"NSURLConnection connection==false");
};
I think you should be bothered reading the documentation. There's a
sendAsynchronousRequest:queue:completionHandler:
method.Check this out: HTTPCachedController
It will help you send POST and GET requests, while it will cache the response and after that it will return the cached data when no internet connection is available.
You will get notified when the data are fetched through a delegate method:
Block code is your friend. I have created a class which does this for you
Objective-C Block code. Create this class here
Interface class
Implementation class
Then you to make this web call, you call it like this
See the setWebCallDidFinish method, it will not be called until the webcall has finished.
Hope that helps!!
Here is some code which I am using in my app:
fyi: stripHTML is a NSString Category to remove HTML tags from JSON --> Found it Here
you can call your content in your class like that:
if you implement it once, you won't want to use synchronous connection again...
Create the connection with
initWithRequest:delegate:startImmediately:
, set yourself as its delegate and implement the delegate methods.