1

Ok, so I have code that posts to my php site, but I want a way to make sure the data gets there. Is there way for the submision to hang intill it timesout or the server responds saying submission accepted?

Here is the code I have so far:

      //Create String
    NSString *var1 =@"waitTime=";
    NSString *var2 =@"&cover=";
    NSString *wait = [var1 stringByAppendingString:waitTime.text];
    NSString *co = [var2 stringByAppendingString:cover.text];


    NSMutableURLRequest *request = 
    [[NSMutableURLRequest alloc] initWithURL:
     [NSURL URLWithString:@"http://mysite.net/index.php"]];

    [request setHTTPMethod:@"POST"];

    NSString *postString = [wait stringByAppendingString:co];

    [request setValue:[NSString 
                       stringWithFormat:@"%d", [postString length]] 
   forHTTPHeaderField:@"Content-length"];

    [request setHTTPBody:[postString 
                          dataUsingEncoding:NSUTF8StringEncoding]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
socbrian
  • 151
  • 1
  • 2
  • 8

2 Answers2

1

You can get the status code as explained in this link

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
   NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
   int code = [httpResponse statusCode];
}

Status codes are defined at this link.

201

Following a POST command, this indicates success, but the textual part of the response line indicates the URI by which the newly created document should be known.

Update

If the connection times out, you should see this when the connection:didFailWithError: delegate method is called.

Community
  • 1
  • 1
Jim
  • 5,940
  • 9
  • 44
  • 91
  • this would be considered a method?, could I call this when a button is clicked? – socbrian Feb 29 '12 at 00:00
  • These are NSURLConnection protocol delegate methods. They are callback methods that your connection makes at different stages in handling the response from the server. You would not want to call them as target:action: responses to a button push. – Jim Feb 29 '12 at 00:04
  • After you implement the delegate methods, come back here if they are not being called after you initiate the request and tell us what happened. You may want to open a new question with the details. – Jim Feb 29 '12 at 00:07
0

Take a look at the documentation for NSURLConnection. In short, what you want to do is implement some of the delegate methods of the NSURLConnectionDelegate protocol; various messages will be sent to the delegate to apprise you of how the load is going.

bdesham
  • 15,430
  • 13
  • 79
  • 123