Questions tagged [urlsession]

URLSession is the API for HTTP connections introduced in iOS 7 and OS X 10.9.

The class URLSession was introduced to Apple's Foundation.framework in iOS 7 and OS X 10.9.

A URLSession object can be considered a group of related data transfer tasks. A response may be received and can be handled via a block statement or delegate methods. URLSession provides status and progress properties, of which support canceling, resuming, or suspending tasks, and resuming suspended, canceled, or failed downloads.


Types of Sessions

URLSession can support three different types of sessions, which is determined by the configuration object used to initialize the session object.

  • Default Sessions
  • Ephemeral Sessions
  • Background Sessions

Types of Tasks

There are three types of tasks, the last two (Download & Upload) of which support background use.

  • Data Tasks - Send and receive data via a NSData object
  • Download Tasks - Retrieves data in the form of a file
  • Upload Tasks - Sends data, most typically in the form of a file

Related tags to explore:

Adapted from Apple's URL Loading System Programming Guide > NSURLSession.

785 questions
4
votes
3 answers

handling HTTP status code with URLSession and Combine

I'm trying to handle the responses that arrive from a DataTaskPublisher reading its response status code. When status code is greater than 299, I'd like to return a ServiceError type as Failure. In every examples that I've seen I've used .mapError…
MatterGoal
  • 16,038
  • 19
  • 109
  • 186
4
votes
2 answers

how to publish the data of a network request using Combine framework and SwiftUI

I'm developing an iOS app using swiftUI and Combine framework and also MVVM. I'm want to handle the login API request in a separate class called LoginService, which is used in the LoginViewModel. Now I want to know how should I publish and observe…
Alireza Moradi
  • 308
  • 3
  • 10
4
votes
2 answers

iOS - HTTP Method not making POST Request but instead making GET

I'm trying to make a POST call in my app using URLSession (and AlamoFire) no luck with either, I'm checking the network call on Charles Proxy and it's always being sent as a GET call even though I've specified the HTTPMethod to be "POST" or .post in…
getswifty
  • 41
  • 1
4
votes
3 answers

Background Upload is not working in Swift

I was trying implement a code where a API will be called shortly after a file has been uploaded into AWS server but it must be in background mode. Whereas the AWS sdk manages uploading file into their server in background mode but the following code…
Poles
  • 3,585
  • 9
  • 43
  • 91
4
votes
0 answers

Trying to understand URLSession dataTask cacheing

I am downloading images from a website using the code below that caches images. Do I need to store images in the apps directory to permanently save them? After running this code, I can turn my device on airplane mode, restart it, and open the app…
Bobby
  • 6,115
  • 4
  • 35
  • 36
4
votes
0 answers

URLSession downloadTask w/ block get download progress

When I conform to the URLSessionDownloadDelegate The following delegate method can be used to get the progress of the download. urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData…
Brandon A
  • 8,153
  • 3
  • 42
  • 77
4
votes
2 answers

how to authenticate the proxy in urlsession?

Hi I'm trying to add proxy with credentials to my App but whenever I'm tried, I got following (In Xcode) console warning: *** WARNING: CFMachPortSetInvalidationCallBack() called on a CFMachPort with a Mach port (0x900b) which does not have any send…
Lion
  • 872
  • 1
  • 17
  • 43
4
votes
2 answers

Swift - Image color resets when Alert is displayed

So in my iOS app I have a menu designed like this: The Images are created with the following code: cell.imageCell?.image = menuItems[indexPath.row].image cell.imageCell.image = cell.imageCell.image?.withRenderingMode(.alwaysTemplate) …
J.Paravicini
  • 882
  • 12
  • 39
3
votes
4 answers

How can I pass parameters in an HTTP Post request in Swift?

Am working on a simple Swift test app which just calls Perl script on my server. Right now I just want to send over a username and id, and get them back in a JSON response. Nothing more, am still in the learning stage. But no matter which way I try,…
Bartender1382
  • 195
  • 1
  • 10
3
votes
1 answer

Backporting URLSession's download(for:delegate:) for concurrent usage

I was trying to backport URLSession's download(for:delegate:) since it requires a deployment target of iOS 15+ but concurrency is now supported for iOS 13+. It seemed like a very straight forward process since you can pass a continuation into the…
ph1psG
  • 568
  • 5
  • 24
3
votes
2 answers

One method in many Tasks async/await

Hi I have a case where I need to call the same method in multiple Tasks. I want to have a possibility to call this method one by one (sync) not in parallel mode. It looks like that: var isReadyToRefresh: Bool = true func refresh(value: Int) async…
PiterPan
  • 1,760
  • 2
  • 22
  • 43
3
votes
2 answers

Can somebody help convert URLSession from Swift to Kotlin

I want to make Multiplatform Weather App with Kotlin. And I also want get weather data separate for Android and iOS in common module. With Android I haven't any problem, but with iOS part I have. I want to use Foundation framework to get and…
3
votes
3 answers

Make multiple network requests on Swift

I am trying to download data from a school course catalog site. I have 64 Urls in the variable UrlBook. I have successfully written code to download a collection of courses and turn them into a single subject object from a single url using…
Jay Gatsby
  • 59
  • 1
  • 3
3
votes
5 answers

Avoiding [weak self] for simple operations?

For short-running operations, is it acceptable to avoid [weak self]? For example, URLSession will retain the closure from dataTask(with:completion:): final class ViewController: UIViewController { let label = UILabel() override func…
fawers
  • 73
  • 3
3
votes
3 answers

iOS 13 URLSessionWebSocketTask Current Connection Status

I have my URLSessionWebSocketDelegate set up and implemented urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) However, it only seems to hit the…
mredig
  • 1,736
  • 1
  • 16
  • 29