What is the easiest way to make an HTTP GET request with iOS 5?

I am trying to send a sentence from my application to a php file on my web server, I tested a php script in my browser that sends an email to the user and saves the sentence in my database, which all works fine. And when I run the following script, I get a successful connection through IOS, but I do not get the results in my database.

NSString *post = [NSString stringWithFormat:@"http://blahblah.com/suggest.php?s=%@&n=%@&e=%@", suggestion, name, email]; // Create the request. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:post] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; // create the connection with the request // and start loading the data NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { NSLog(@"Connection establisted successfully"); } else { NSLog(@"Connection failed."); } 

I checked all the lines and encoded all the spaces with% 20 etc. Can anyone see any obvious reason why my script will not work?

What is the easiest way to make an HTTP request from my application without opening a safari?

+4
source share
1 answer

The problem is that you are creating a connection but not sending the actual "connect" request. Instead

 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

try using this piece of code:

 NSURLResponse* response = nil; NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil] 

This is a quick and dirty solution, but keep in mind that while this connection is in progress, your UI thread will be frozen. The way around it is to use the asynchronous connection method, which is slightly more complicated than the above. An Internet search for NSURLConnection sends an asynchronous request - there is a response.

+7
source

Source: https://habr.com/ru/post/1395043/


All Articles