-2

I need to connect to an API via a GET request. I have a string that will be the URL. What is the current best practice way on iOS 7 to send that GET request over HTTPS, and store the string in a string variable?

I just need to make a simple request. There is no session data involved. No cookies. Just a simple GET from a URL and it returns me a string in the response body. Thanks!

CommaToast
  • 11,370
  • 7
  • 54
  • 69
  • i guess google didn't help you? – Leijonien Nov 25 '13 at 08:28
  • possible duplicate of [How to send a Get request in iOS?](http://stackoverflow.com/questions/6212453/how-to-send-a-get-request-in-ios) – KIDdAe Nov 25 '13 at 08:29
  • Yes but how do you make it an HTTPS request, as opposed to regular HTTP? I tried the existing methods and they simply return the error "bad URL". But if I paste the URL in Safari, it works. I'd like to point out that the only method in the post you link to uses NSURLConnection, which is deprecated, and I asked for the "current best practice way on iOS 7" which means using NSURLSession. So it would be nice if you'd stop downvoting me thanks. – CommaToast Nov 25 '13 at 10:04

1 Answers1

0

First you need to implement connection delegate methods. Following is the code for making http request.

NSString *strURL = [NSString stringWithFormat:@"https://your_url"];
NSURL *url = [NSURL URLWithString:strURL];
NSLog(@"URL Request : \n %@",url);

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req addValue:@"your API Key" forHTTPHeaderField:@"Api-Key"];
[req setHTTPMethod:@"GET"];

conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    webData = [[NSMutableData data] retain];
}
Mahesh
  • 526
  • 1
  • 6
  • 21
  • In iOS 7 and later or OS X v10.9 and later, NSURLSession is the preferred API for new code that performs URL requests. – CommaToast Nov 25 '13 at 09:54
  • That's according to Apple. I asked for "current best practice", which your answer is not, even if it works. Thanks though! – CommaToast Nov 25 '13 at 10:01