urlsession

how to wait for the URLSession to finish before returning the result from a function in Swift 3

ε祈祈猫儿з 提交于 2019-12-05 07:15:38
问题 Hi i have a beginner question, where i cant find a good solution for in Swift 3. I hope someone is able to hlep. i have the following code that will check a rest api if the user credentials are valid or not. I want it to wait for the resquest is finished and then return true or false. now it is being send async. Also any improvement in the way i check the JSON value's would be welcome too. func CheckUsernamePassword(username :String ,code:String )-> bool { var validCredentials = false let

Downloading in background and waking app when finished

房东的猫 提交于 2019-12-04 21:42:27
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? There is indeed :) You probably need to change a few things in your setup. First the

How to load image asynchronously with Swift using UIImageViewExtension and preventing duplicate images or wrong Images loaded to cells

☆樱花仙子☆ 提交于 2019-12-04 19:55:38
Im developing an image loading library in Swift 4 something like Kingfisher with some extensions to support loading images from URL into an UIImageView . So then i can use this extension on a UICollection or UITableview cell with an UIImageView like this : let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncell", for: indexPath) if let normal = cell as? CollectionViewCell { normal.imagview.loadImage(fromURL:imageURLstrings[indexPath.row]) } Basically this is my extension Code , implementing the basic URLSession dataTask and Caching public extension UIImageView {

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

有些话、适合烂在心里 提交于 2019-12-04 18:33:12
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.transition(toImage: cachedImage as? UIImage) //self.image = cachedImage return } // download let url =

How to sync serial queue for URLSession tasks?

泄露秘密 提交于 2019-12-04 07:04:50
Using XCode-8.2.1, Swift-3.0.2 and iOS-10.2.1, I am trying to call two different URLSession.shared.dataTasks (the first is a simple URL-request and the second is a POST-request). Since my first dataTask delivers a result that is needed in the httpBody of the second dataTask, the two URLSession.shared.dataTasks shall run in series, one after the other! (and also the preparative code shall run consecutively). I tried, so far, using two consecutive serialQueue.sync{} queues. But I had to realize that the code does not perform in the order I would like to. The print-statement in the log turn out

URLSession error

不想你离开。 提交于 2019-12-02 08:08:57
问题 I am using a function which was actually written for swift 2. I've made moderations for swift 3. But I keep getting the error: URLSession' produces '()', not the expected contextual result type 'URLSession! My code looks like this: func downloadItems() { let url: NSURL = NSURL(string: urlPath)! var session: URLSession! let configuration = URLSessionConfiguration.default session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) let task = session.dataTask(with: url

URLSession error

安稳与你 提交于 2019-12-02 06:52:07
I am using a function which was actually written for swift 2. I've made moderations for swift 3. But I keep getting the error: URLSession' produces '()', not the expected contextual result type 'URLSession! My code looks like this: func downloadItems() { let url: NSURL = NSURL(string: urlPath)! var session: URLSession! let configuration = URLSessionConfiguration.default session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) let task = session.dataTask(with: url as URL) task.resume() } Anybody know what I am doing wrong? instead of : session = URLSession

Using URLSession and background fetch together with remote notifications using firebase

心已入冬 提交于 2019-12-02 05:16:24
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 backgroundSessionCompletionHandler: (() -> Void)? lazy var downloadsSession: Foundation.URLSession = { let configuration =

Swift http request use urlSession

青春壹個敷衍的年華 提交于 2019-11-30 06:01:04
问题 I want to write func for HTTP Request to my server and get some data, when i print it (print(responseString)) it looks good, but when i try to return data, its always empty public func HTTPRequest(dir: String, param: [String:String]?) -> String{ var urlString = HOST + dir + "?" var responseString = "" if param != nil{ for currentParam in param!{ urlString += currentParam.key + "=" + currentParam.value + "&" } } let url = URL(string: urlString) let task = URLSession.shared.dataTask(with: url!)

Swift http request use urlSession

北慕城南 提交于 2019-11-28 13:10:48
I want to write func for HTTP Request to my server and get some data, when i print it (print(responseString)) it looks good, but when i try to return data, its always empty public func HTTPRequest(dir: String, param: [String:String]?) -> String{ var urlString = HOST + dir + "?" var responseString = "" if param != nil{ for currentParam in param!{ urlString += currentParam.key + "=" + currentParam.value + "&" } } let url = URL(string: urlString) let task = URLSession.shared.dataTask(with: url!) { data, response, error in guard error == nil else { print("ERROR: HTTP REQUEST ERROR!") return }