Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a Get request in iOS?

I am making a library to get response from a particular URL with specified data and method type. For this, I am making a request with url. But when I set its method type, it shows an exception of unrecognized selector send in [NSURLRequest setHTTPMethod:] I am setting it as

[requestObject setHTTPMethod:@"GET"]; 

Tell me what could be the problem. Also provide me the code if you have.

like image 781
Sanchit Paurush Avatar asked Jun 02 '11 09:06

Sanchit Paurush


People also ask

How do you make HTTP requests with URLSession in Swift?

Before we start, we need to define the URL of the remote image. import UIKit let url = URL(string: "https://bit.ly/2LMtByx")! The next step is creating a data task, an instance of the URLSessionDataTask class. A task is always tied to a URLSession instance.

What is URLSession in Swift?

Overview. The URLSession class and related classes provide an API for downloading data from and uploading data to endpoints indicated by URLs. Your app can also use this API to perform background downloads when your app isn't running or, in iOS, while your app is suspended.

What is API calling in Swift?

This is what happens with API calls. You send a URL request to a server, asking it for some data. You hope the server returns the data quickly, but you don't know how long it will take. Instead of making your user wait patiently for the server to give you the data, you use a completion handler.


1 Answers

NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL              URLWithString:serverAddress]              cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData                                                    timeoutInterval:10  ];  [request setHTTPMethod: @"GET"];  NSError *requestError = nil; NSURLResponse *urlResponse = nil;   NSData *response1 =         [NSURLConnection sendSynchronousRequest:request                          returningResponse:&urlResponse error:&requestError]; 
like image 161
Mobile Developer iOS Android Avatar answered Sep 20 '22 14:09

Mobile Developer iOS Android