A couple of thoughts:
You NSLog
your webData
, which will always show <>
(as your logging it immediately after instantiated it). I'm not sure why you're logging that.
The question is whether you're seeing that <>
, or the NSLog
in connectionDidFinishLoading
.
I ask that because you are not logging the error
in connection:didFailWithError:
, if it fails, you'll never know why. You really should log the error
in connection:didFailWithError:
so you know if it failed, and if so, why:
NSLog(@"%s: %@", __FUNCTION__, error);
In your connection:didReceiveResponse:
, you really should look at the HTTP status code:
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200)
NSLog(@"%s: status code is %d; should be 200", __FUNCTION__, statusCode);
}
If it's not 200, you really want to know about that.
You report in one of your comments that you are seeing connectionDidFinishLoading:
called, but never having didReceiveData
called. That means (unsurprisingly) that there was no data received. So, you should:
Confirm that the connection:didReceiveResponse:
reported a statusCode
of 200
; and
Confirm that the server code is working properly. I could imagine getting the behavior you describe if you had an error in your server PHP code (which is exacerbated by the fact that servers often have display_errors
turned off in their php.ini
file).
As an aside, if it's possible that the value associated with key1
might contain any reserved characters (as defined in section 2 of RFC 3986), you should percent-escape the string using CFURLCreateStringByAddingPercentEscapes
. Thus:
NSString *post = [NSString stringWithFormat:@"key1=%@", [self percentEscapeString:vali]];
Where, per the W3C specs for application/x-www-form-urlencoded
, you not only percent escape, but also replace spaces with +
characters, thus:
- (NSString *)percentEscapeString:(NSString *)string
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
(CFStringRef)@" ",
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}