NSURLSession bandwidth limiting

2019-07-16 14:36发布

问题:

Is there a way to limit the bandwidth in NSURLSession?

I'm making file-sync-client for macOS like Dropbox/GoogleDrive/pCloud and they all have bandwidth limiting options, but I'm not sure how to configure NSURLSession to respect bandwidth limiting.

回答1:

Unless Apple has added something very recently, NSURLSession provides no facilities for bandwidth limiting. The only ways I'm aware of to do that are:

  • Use lower-level APIs that allow you to provide your own sockets and then throttle the data rate at the TCP socket level.
  • Provide an in-app web proxy and use it for all outgoing requests. Configure the proxy to limit the bandwidth of all requests that go through it.


回答2:

Looks like ASIHTTPRequest features Bandwidth throttling support. Although it may be an old library, from what I observe it's still fully functional thus it may be worth exploring.

Of course, using ASIHTTPRequest would require abandoning NSURLSession for this purpose.