urlsession

How to mock URLSession.DataTaskPublisher

喜欢而已 提交于 2020-06-27 12:06:16
问题 How can I mock URLSession.DataTaskPublisher ? I have a class Proxy that require to inject a URLSessionProtocol protocol URLSessionProtocol { func loadData(from url: URL) -> URLSession.DataTaskPublisher } class Proxy { private let urlSession: URLSessionProtocol init(urlSession: URLSessionProtocol) { self.urlSession = urlSession } func get(url: URL) -> AnyPublisher<Data, ProxyError> { // Using urlSession.loadData(from: url) } } This code was originally used with the traditional version of

Using URLSession and background fetch together with remote notifications using firebase

天大地大妈咪最大 提交于 2020-01-21 19:15:48
问题 I am trying to implement a basic func here which will be called when my app is backgrounded or suspended. In reality, we aim to send about 5 a day so Apple should not throttle our utilisation. I've put together the following which uses firebase and userNotifications, for now, it is in my app delegate. import Firebase import FirebaseMessaging import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var

Basic Authentication in Swift 3 does't work

跟風遠走 提交于 2020-01-05 05:36:07
问题 I am struggling with basic authentication in Swift. I have a Rest back end service over SSL and with basic authentication. My objective-c client code works well but the corresponding Swift one doesn't work because the authentication fails. This is the Swift code: let sUrl = "HTTPS://localhost:8443/Test_1/rest/Service/returnInfo" let url: URL = URL(string: sUrl)! let request: URLRequest = URLRequest(url: url); let session: URLSession = URLSession(configuration: URLSessionConfiguration.default,

Is there a way to request multiple distinct resources in parallel using URLSession.shared.dataTask

十年热恋 提交于 2020-01-01 19:44:30
问题 I found this piece of code here on how to download images simultaneously without any breakages, func loadImageRobsAnswer(with urlString: String?) { // cancel prior task, if any weak var oldTask = currentTask currentTask = nil oldTask?.cancel() // reset imageview's image self.image = nil // allow supplying of `nil` to remove old image and then return immediately guard let urlString = urlString else { return } // check cache if let cachedImage = DataCache.shared.object(forKey: urlString) { self

Converting ecobee Alamofire request to use URLSession

随声附和 提交于 2019-12-25 03:29:18
问题 As a followup to my last question (Alamofire syntax for ecobee request), I would prefer to just use URLSession for the request. Now I'm back to a request that times out with status 408 using the following code: guard let url = URL(string: "https://api.ecobee.com/1/thermostat") else { return } let jsonParameters = [ "selection": [ "selectionType": "registered", "selectionMatch": "" ] ] let jsonData = try! JSONEncoder().encode(jsonParameters) let jsonString = String(decoding: jsonData, as: UTF8

how to pass properties to swift 3 urlsession didFinishDownloadingTo delegate after background download?

若如初见. 提交于 2019-12-24 19:37:52
问题 I can successfully download a zip file in background with my app code with ios 10 and swift 3. I use a backgroundsession data task: let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: (prefix + postfix)) let backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main) var request = URLRequest(url: dlUrl ) request.httpMethod = "GET" request.cachePolicy = NSMutableURLRequest

Failing to Upload Picture with multipart/form-data to a Server

霸气de小男生 提交于 2019-12-23 03:04:33
问题 I've read quite a lot of topics here on multipart/form-data . It still doesn't work. I am able to upload a file to my server with URLSession.shared.uploadTask . class MainViewController: UIViewController { @IBOutlet weak var pictureView: UIImageView! @IBAction func postTapped(_ sender: UIButton) { postData() } func postData() { var request = URLRequest(url: URL(string: "http://www.mywebsite.com/upload.php")!) request.httpMethod = "POST" request.timeoutInterval = 30.0 guard let imageData =

Downloading in background and waking app when finished

假装没事ソ 提交于 2019-12-22 01:13:06
问题 I use URLSession configured as background session to download some files. When download finishes I use: func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) to move downloaded file. Everything works fine, except that when app is in the background this function is not being called. It's called right after app is restored to foreground, but I would like the system to wake my app after file is downloaded. Is there some way to do this?