iPhone - How to find NSURLConnection started or not? - iphone

There's a delegate in NSURLConnection "- (void)connectionDidFinishLoading:(NSURLConnection *)connection"
This will be called when the connection finishes.
But is there any delegate or way to know when the connection has started.

What you're looking for is the NSURLConnection delegate method connection:willSendRequest:redirectResponse:. It will be called once when the connection starts, but be warned that it will be called again for every redirect response (if there are any).

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
can be used.

Related

viewDidAppear being called before WebService connection methods

ViewDidAppear method I put breakpoint in the last step of the operation, but the breakpoint viewDidAppear method of putting at first, trying to run it directly.
Are emerging in connection using WebService. Be the first breakpoint while running webservice connections, calling the latest viewDidAppear. However, prior to providing breakpoint viewDidAppear when calling WebService connection, and this causes the value NULL to return.
In short, I would like to be called viewDidAppear method, after obtaining all the webservice connections. Breakpoint when it's like this, but when I want to work in the same way.
- (void)viewDidAppear:(BOOL)animated
{
[self LabelYukle];
[super viewDidAppear:animated];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if(theConnection)
[webData setLength:0];
}
viewDidAppear is called by iOS system on its own just before view is going to be appeared.
If you want to perform some functions after didReceiveResponse method, there is a method in NSURLConnectionDelegate that can help you.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
This method is used when a connection has finished loading successfully and you can write the functions you want to perform after didRecieveRespose here in this method.
You can read more about NSURLConnectionDelegateProtocol methods here.

Track redirects when using sendAsynchronousRequest:queue:completionHandler:?

How can I invoke custom code when a redirect happens while sending a request with the method:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
?
I'm looking to replicate the behavior the NSURLConnectionDelegate method:
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
I don't think you can replicate the behavior, since sendAsynchronousRequest only calls the completion handler given to it in the code block.
It seems that you must use the delegate method for this purpose. I don't see any benefit from using sendAsynchronousRequest over the normal NSURLConnection, except for the fact that you have to keep an ivar of NSMutableData and possibly NSURLConnection if there are multiple of them per instance of the class.

checking condition after response

I am sending a request and on receiving the response i need to check the condition and only then navigate to the next screen but then this condition is checked even before i receive the response. How can i check the condition only after receiving the response. Thanks in advance.
Are you using NSURL Connection or any Component??
Try using NSURL Connection delegates. Check out this link:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/cl/NSURLConnection .
I think any of the two methods will work.
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data OR
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
I would consider using the ASI library. It is nicer to use than NSURLConnection.
Either way, you should start the request asynchronously and set the delegate to self.
When you receive the response in the delegate callback you can then continue to the next screen.
For ASI you would do ...
- (void)dorequest
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
// Do no do anything else in this method.
// wait for the response to call your delegate method
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self loadNextScreen];
}
NSURLConnection would use similar code. Just make sure you use the asynchronous method.
i feel the problem wasn't explained properly because it was misunderstood. I am using NSURLConnection and the request is an asynchronous one, so that was not the question. I needed some code to be executed only after the data download was completed.
I got an answer; by using NSNotifications. In my parser class within connectionDidFinishLoading:(NSURLConnection *)connection method i post Notifications. In my viewcontroller class i added an observer where i execute the needed code.
For more help see : http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/Reference/Reference.html
Thanks

Cocoa Touch - Display an Activity Indicator while loading a UITabBar View

I have a UITabBar Application with two views that load large amounts of data from the web in their "viewWillAppear" methods. I want to show a progress bar or an activity indicator while this data is being retrieved, to make sure the user knows the app isn't frozen.
I am aware that this has been asked before. I simply need some clarification on what seems to be a rather good solution.
I have implimented the code in the example. The question's original asker later solved their problem, by putting the retrieval of data into another "thread". I understand the concept of threads, but I do not know how I would impliment this.
With research, I have found that I need to move all of my heavy data retrieval into a background thread, as all of the UI updating occurs in the main thread.
If one would be so kind as to provide an example for me, I would be very appreciative. I can provide parts of my existing code as necessary.
If you use NSURLConnection it runs on another thread automatically.
in your viewDidLoad:
NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
then you need some custom methods. If you type in -connection and press Esc you'll see all the different methods you can use. There are three you will need with this:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// this is called when there is a response
// if you're collecting data init your NSMutableData here
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// each time the connection downloads a
// packet of data it gets send here
// so you can do [myData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
// the connection has finished so you can
// do what you want with the data here
}
That is basically all there is to it. NSURLConnection handles all the multithreading itself and you don't have to worry. Now you can create an activity indicator and display it and it will work because the main thread is empty. :)

My delegate's methods never get called when I use connectionWithRequest:delegate

I'm making an asynchronus POST request in an iPhone app with this call:
[NSURLConnection connectionWithRequest:req delegate:self];
The request seems to get to the server just fine, but none of the delegate methods get hit. I've implemented:
- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
and
- (void) connection:didFailWithError:(NSURLConnection *)connection
None of the log messages for these methods ever appear. The documentation for connectionWithRequest:delegate: says:
delegate
The delegate object for the connection. The delegate will receive delegate messages as the load progresses. Messages to the delegate will be sent on the thread that calls this method. For the connection to work correctly the calling thread’s run loop must be operating in the default run loop mode.
I think the call is being made off of the main thread (is the assumption that, if I didn't start a new thread explicitly, everything runs in one thread correct?), and I checked the thread with this:
CFRunLoopRef loop = CFRunLoopGetMain();
CFStringRef modeString = CFRunLoopCopyCurrentMode(loop);
NSLog(#"The main loop is running in mode: %#", modeString);
Which creates this console output:
2009-09-13 13:32:05.611 Stock Footage[686:20b] The main loop is running in mode: kCFRunLoopDefaultMode
So, that sounds like it is in the default run loop mode. Is there anything else I should look at that could tell me why my delegate methods aren't hit? The delegate is definitely not dying before the request is complete.
Update: CFRunLoopGetCurrent has the mode kCFRunLoopDefaultMode.
Update (5:40 PM Eastern): Well, I've switched to using initWithRequest:delegate: startImmediately in order to get the callbacks going, but I'd still love to see an example of connectionWithRequest:delegate that actually hits the delegate up.
I had a similar problem. The reason my delegates were not being called is that after I called connectionWithRequest, I started a loop that was checking if the data from the connection had been sent. Since I never returned the control to the RunLoop code, the messages left in the queue were never processed, and therefore the delegate's methods were not called either...
Just make sure that you do not do anything heavy after calling connectionWithRequest and everything should work nicely.
Two things jump out at me:
You have the wrong prototype for your error method (should still get called, however):
connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
You should check to make sure you're getting a valid connection back from + connectionWithRequest:delegate:. It's possible it's not even starting the request.