Making GET and POST Requests from an iPhone Applic

2019-02-03 19:34发布

I'm following along with this useful looking answer to my question.

It seems to be working, but here are two questions I have:

  1. How do I detect an HTTP error? The didFailWithError method doesn't seem to be getting called?

UPDATE: It considers any response a success. So I guess I have to handle HTTP errors in the didRecieveResponse method, but besides telling the user there was an error when I hit an HTTP error, do I need stop the connection somehow? And/or cleanup?

  1. I see this line in the answer:
[[NSURLConnection alloc] initWithRequest:request delegate:self];

Do I need to release that? Where, how, when?

3条回答
够拽才男人
2楼-- · 2019-02-03 20:06

Yes, you need to release that object. See the Memory Management Programming Guide for Cocoa. Basically, if you ever create an object with a method name that begins with alloc or new or contains copy, you become an owner of the object and are responsible for freeing it later. Only in the case where you know you're going to need the object up until program termination is it ok not to free it, in which case the operating system reclaims the memory when your app terminates.

If you only need the object within a small scope, you can send it the autorelease message. This will add it to the autorelease pool. The autorelease pool periodically sends a release message to each object in it. It's kind of complicated; see the section on autorelease pools. For example:

In this case, though, because NSURLConnections are asynchronous, autoreleasing won't work. You don't know exactly when it's going to call back into your object with data, so you want to make sure the object hasn't been released yet. The only way to be sure is to know exactly when you're done with the object, and then send it a release message yourself.

All of the various init* functions return a pointer to the given object, so you can just do:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
...
// when done with connection:
[connection release];
查看更多
一纸荒年 Trace。
3楼-- · 2019-02-03 20:08

You will get the status code returned in the didReceiveResponse

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSHTTPURLResponse *httpResponse;
    httpResponse = (NSHTTPURLResponse *)response;
    int statusCode = [httpResponse statusCode];
    //statusCode will be the http code returned like 201,500
 }

For stopping the connection, use a class-level variable for the connection. The best way to go about it would be create a wrapper which sends requests and receives response. Make your viewcontroller a delegate of this class and whenever the didReceiveResponse gives an error status code, call the appropriate method of the delegate and stop the connection.

Here's a good wrapper class example

http://kosmaczewski.net/projects/objective-c-rest-client/

查看更多
贪生不怕死
4楼-- · 2019-02-03 20:12

Answering the question in your "update"...

Immediately autorelease the NSURLConnection. The connection object is retained by the NSRunLoop (to which it adds itself automatically unless you use the startImmediately:NO constructor). It will get automatically removed from run loop (and hence dealloc'd) on error or finish.

查看更多
登录 后发表回答