filmov
tv
|Networking with URL Session DataTask (GET and POST)|Swift 5, #xcode NSURLSession using Swift HTTP|
Показать описание
#codeindia
Making HTTP requests in iOS using URLSession.
There are three types of NSURLSession:
a) default sessions: behaviour like NSURLConnection
b) ephemeral sessions: not cache any content to disk
c) download sessions: store the result in file and transferring data even when app is suspended, exits or crashes
Based on above sessions, developers can schedule three types of tasks:
a) data tasks: retrieve data to memory
b) download tasks: download file to disk
c) upload tasks: uploading file from disk and receiving response as data in memory
We can implement a simple HTTP request using Swift Playground:
CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
The flow of authentication will choose the handle delegate, if you didn’t implement the session level delegate
(URLSession:didReceiveChallenge:completionHandler:), it will pass to task level delegate(URLSession:task:didReceiveChallenge:completionHandler:).
That means we need to have delegate implementation since the default system delegate didn’t help us to handle these behavior.
Next, we should implement the didReceiveChallenge for listening challenge request from server. There are many ways to response challenge to server, some may use the username/password or other way to create credential, we just use serverTrust here and pass the NSURLCredential into completion. (URLSession(_:didReceiveChallenge:completionHandler:)pes of NSURLSession:
a) default sessions: behaviour like NSURLConnection
b) ephemeral sessions: not cache any content to disk
c) download sessions: store the result in file and transferring data even when app is suspended, exits or crashes
Based on above sessions, developers can schedule three types of tasks:
a) data tasks: retrieve data to memory
b) download tasks: download file to disk
c) upload tasks: uploading file from disk and receiving response as data in memory
Making HTTP requests in iOS using URLSession.
There are three types of NSURLSession:
a) default sessions: behaviour like NSURLConnection
b) ephemeral sessions: not cache any content to disk
c) download sessions: store the result in file and transferring data even when app is suspended, exits or crashes
Based on above sessions, developers can schedule three types of tasks:
a) data tasks: retrieve data to memory
b) download tasks: download file to disk
c) upload tasks: uploading file from disk and receiving response as data in memory
We can implement a simple HTTP request using Swift Playground:
CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
The flow of authentication will choose the handle delegate, if you didn’t implement the session level delegate
(URLSession:didReceiveChallenge:completionHandler:), it will pass to task level delegate(URLSession:task:didReceiveChallenge:completionHandler:).
That means we need to have delegate implementation since the default system delegate didn’t help us to handle these behavior.
Next, we should implement the didReceiveChallenge for listening challenge request from server. There are many ways to response challenge to server, some may use the username/password or other way to create credential, we just use serverTrust here and pass the NSURLCredential into completion. (URLSession(_:didReceiveChallenge:completionHandler:)pes of NSURLSession:
a) default sessions: behaviour like NSURLConnection
b) ephemeral sessions: not cache any content to disk
c) download sessions: store the result in file and transferring data even when app is suspended, exits or crashes
Based on above sessions, developers can schedule three types of tasks:
a) data tasks: retrieve data to memory
b) download tasks: download file to disk
c) upload tasks: uploading file from disk and receiving response as data in memory
Комментарии