Lennie
Lennie

Reputation: 2069

NSURLRequest/NSURLConnection not working?

I got the code below to call a PHP script, for some reason it say that the connection is successful but it actually didn't call my script since I haven't received the email :-(

None of the NSURLConnectionDelegate methods get called.

Testing on iPhone 5.x device and Simulator.

Any suggestions/ideas? Thank you very much!

NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];

NSLog(@"web service URL: %@", url);

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest: request delegate:self];
NSLog(@"connection: %@", [connection debugDescription]);
if (connection) {
    NSLog(@"Connection succeeded");
} else {
    NSLog(@"Connection failed");
}

Upvotes: 1

Views: 3946

Answers (3)

Eric
Eric

Reputation: 4061

Are you using the specific delegate functions for NSURLConnection?

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Did Receive Response %@", response);
    responseData = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    //NSLog(@"Did Receive Data %@", data);
    [responseData appendData:data];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    NSLog(@"Did Fail");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Did Finish");
    // Do something with responseData
}

Upvotes: 0

jdl
jdl

Reputation: 6323

NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];

NSLog(@"web service URL: %@", url);

//add the following or something like
[url setString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

//modify the following    
connection = [[NSURLConnection alloc] initWithRequest: request delegate:self startImmediately:YES]; 
NSLog(@"connection: %@", [connection debugDescription]);
if (connection) {
    NSLog(@"Connection succeeded");
} else {
    NSLog(@"Connection failed");
}

Upvotes: 2

jerrylroberts
jerrylroberts

Reputation: 3454

I'd be willing to bet that you have some content in your email body that's not safe to send through the url

NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];

So say your email body contains &, that would break the code you have. You either need to escape the message body or send it through the request body instead of passing it through the URL. I could be wrong, but I bet that's what it is. Your code looks fine, I bet its the data ;)

Upvotes: 0

Related Questions