Network Communication (HTTP Layer) - iOS
Overview
This page guides you on how to use HTTP Layer and its classes to help with working with REST API calls. This will also provide a way to get the response back via the callback methods. Using this, you can work with your GET and POST REST API methods and get the response as a callback. Please notice, we are not supporting the Image or File downloading approach using this.
This section describes the methods required by the mobile app for communicating with server(s), for transmitting or receiving data over REST APIs.
Note: This layer does not support image or file downloads.
HTTP layer works with the app foreground state by default. To enable the HTTP layer for transmitting data in the app background state, refer this section.
Creating an HTTP request
Creating a request object
To serve the REST API calls, create an ORARequest object. The following are the parameters of the ORARequest object.
Parameter | Description | |
1 | url | A URL for the REST call, which is a string |
2 | requestType | A typedef enum to specify the request type. This accepts two values. i.e POST or GET. The default value is POST |
3 | payload | An optional payload for the request which accepts NSDictionary |
4 | requestBody | An optional requestBody as NSData. This is useful when we need parameter encoding. Either requestBody or payload can be passed to ORARequest. In case of both, the priority can be given to requestBody property |
5 | headers | Headers for the request in a Dictionary format |
6 | compression | A boolean value to specify whether compression is needed or not. The default value is false |
7 | retryCount | A retry count for the request. This is needed in case of internet or server failures. The default value is 1. The minimum acceptable value is 0 and the maximum is 15 |
8 | priority | A typedef enum to specify the priority of the call. This accepts two values. i.e Immediate or Normal. The default value is Normal |
9 | requestTag | An optional request Tag to identify from where the response is coming when the callback is triggered |
ORARequest *request = [[ORARequest alloc]initWithURL:@"www.abc.come/xyz"];
request.requestType = POST;
request.payload = payload;//payload in NSDictionary to be added
request.headers = headers;//headers in NSDictionary to be added
request.priotity = Immediate;
let request = ORARequest(url: "www.abc.come/xyz")
request.requestType = POST
request.payload = payload //payload in NSDictionary to be added
request.headers = headers //headers in NSDictionary to be added
request.priotity = Immediate
Triggering the request
After the ORARequest
object is created, you can use ORAHTTPManager addRequest:completionHandler:
method to trigger the request. The completionHandler
uses ORAResponse
as a parameter. The following are the different properties of ORAResponse
class.
Property | Description | |
1 | response | An NSURLResponse object that provides response metadata, such as HTTP headers and status codes. If you are making an HTTP or HTTPS request, the returned object is actually an NSHTTPURLResponse object |
2 | data | The data that is returned by the server and it is of NSData type |
3 | error | An error object that indicates why the request failed, or nil if the request was successful |
4 | requestTag | An optional request Tag to identify from where the response is coming when the callback is triggered |
[[ORAHTTPManager sharedManager] addRequest:request
completionHandler:^(ORAResponse * _Nonnull response) {
//Handle the response
}];
ORAHTTPManager.shared().addRequest(request, completionHandler: { response in
//Handle the response
})
Execute Request when the app is in the background
When an app is backgrounded, the HTTP requests in the queue are paused. If an HTTP request is under execution when the app is backgrounded, the request is executed and subsequent requests in the queue are paused. Once the app is resumed and is foregrounded, the HTTP requests resume executing. This is the default.
The default behavior of the queue can be overridden by using ORAConfigHTTPBackgroundTaskEnabled
configuration. The ORAConfigHTTPBackgroundTaskEnabled
configuration is set to false by default
. If the ORAConfigHTTPBackgroundTaskEnabled
configuration is set to true, then all HTTP requests are processed, even if the application is backgrounded. The ORAConfigHTTPBackgroundTaskEnabled
uses UIBackgroundTaskIdentifier
to support background task execution. For more details about extending background time and its implementation, see Apple’s Extending Your App’s Background Execution Time page.
ORACoreDataContainer *coreContainer = [[ORACoreDataContainer alloc] init];
[coreContainer putValue:@"true" forKey:[ORAConfigHTTPBackgroundTaskEnabled new]];
let coreContainer = ORACoreDataContainer()
let backgroundExecution = ORAConfigHTTPBackgroundTaskEnabled()
coreContainer.putValue("true", forKey: backgroundExecution)