Try this out:
import Foundation
import Network
class CellularRequest {
static func execute(url: URL,
httpMethod: String,
postBody: String? = nil,
completion: @escaping (_ error: NWError?, _ httpBody: String?, _ httpHeaders: String?)
-> Void) {
guard let urlHost = url.host else { return }
let host = NWEndpoint.Host(urlHost)
let tlsOptions = NWProtocolTLS.Options()
let tcpOptions = NWProtocolTCP.Options()
let parameters = NWParameters(tls: tlsOptions, tcp: tcpOptions)
parameters.prohibitExpensivePaths = false
#if targetEnvironment(simulator)
parameters.requiredInterfaceType = .loopback
#else
parameters.requiredInterfaceType = .cellular
#endif
let connection = NWConnection(host: host, port: .https, using: parameters)
connection.stateUpdateHandler = { newState in
switch newState {
case .ready:
// The connection is ready; you can now send an HTTP request
var httpRequest = "\(httpMethod) \(url.path) HTTP/1.1\r\nHost: \(urlHost)\r\n"
if let postBody = postBody {
httpRequest += "Content-Type: application/json\r\n"
httpRequest += "Content-Length: \(postBody.utf8.count)\r\n\r\n"
httpRequest += postBody
} else {
httpRequest += "\r\n"
}
let requestData = httpRequest.data(using: .utf8)
connection.send(content: requestData, completion: .contentProcessed { error in
if let error = error {
completion(error, nil, nil)
} else {
connection.receive(minimumIncompleteLength: 1, maximumLength: .max) { data, _, _, error in
if let data = data {
if let response = String(data: data, encoding: .utf8),
response.contains("\r\n\r\n") {
// The headers are complete, and we have the full response
let headersAndBody = response.components(separatedBy: "\r\n\r\n")
completion(nil, headersAndBody.first, headersAndBody.last)
}
} else if let error = error {
completion(error, nil, nil)
}
}
}
})
case let .failed(error), let .waiting(error):
completion(error, nil, nil)
default:
break
}
}
// Start the connection
connection.start(queue: DispatchQueue.global())
}
}