Advertisement
Larme

Untitled

Jun 16th, 2022
1,686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.95 KB | None | 0 0
  1. class Connection: NSObject, URLSessionDataDelegate {
  2.  
  3.     lazy var session: URLSession = {
  4.         URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
  5.     }()
  6.  
  7.     func start() {
  8.         let baseURL = ""
  9.         let keyID = ""
  10.         let url = URL(string: "\(baseURL)/v2/path/\(keyID)")!
  11.  
  12.         var urlRequest = URLRequest(url: url)
  13.         urlRequest.setValue("application/x-ndjson", forHTTPHeaderField: "Accept")
  14.         let tasks = session.dataTask(with: urlRequest)
  15.         tasks.resume()
  16.     }
  17.  
  18.     func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
  19.         print("ReceivedData: \(data) - \(String(data: data, encoding: .utf8))") //But since it's chunk, the conversion to String might fail
  20.     }
  21.  
  22.     func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
  23.         print("didComplete withError: \(error)")
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement