Xcode8 Beta 6 - Urlsession with Completionhandler Argument Not Working

Xcode8 beta 6 - URLSession with completionHandler argument not working

I found out that I was calling handleTaskResult which had error as NSError?, but error is now Error?, after changing this, it compiles!

urlSession:dataTask:didReceive:completionHandler not called in Xcode8 Swift3

Direct quote from Apple SDK regarding this method:

/*
* Messages related to the operation of a task that delivers data
* directly to the delegate.
*/
@protocol NSURLSessionDataDelegate <NSURLSessionTaskDelegate>
@optional
/* The task has received a response and no further messages will be
* received until the completion block is called. The disposition
* allows you to cancel a request or to turn a data task into a
* download task. This delegate message is optional - if you do not
* implement it, you can get the response as a property of the task.
*
* This method will not be called for background upload tasks (which cannot be converted to download tasks).
*/
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler;

Edit: added more context to a quote

URLSession stopped working on WatchOS 6, did something change from OS 5?

After a lot of debugging, I've noticed that you're supposed to check the "Supports Running Without iOS App Installation" checkbox under Project > Watch Extension > General > Deployment Info.
Check that checkbox so the Watch Simulator has internet connection.
On Xcode beta, the watch simulator never boots with the cellphone simulator as it did on Xcode 10.3. If you don't check that box, the watch will never have internet connection, as it depends on a cellphone that isn't there.

I hope other users find the information useful!

Cannot invoke 'dataTask' with an argument list of type

For some reason adding:

return () 

at the end of the closure, as suggested on the Apple forum, fixed the issue.

URLSession issue

The initializer you are looking for URLSession is like init(configuration:delegate:delegateQueue:). So change your initialization of session like this.

let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)

ambiguous reference to member datatask swift Xcode 8

You can convert your NSURL to a URL using .absoluteURL

guard let url = URL.absoluteURL else { return }

URLSession.shared.dataTask(with: url) { (data, response, error) in
//
}.resume()

UPDATE

Your completion block has the wrong types. You're using NSData instead of Data and NSError instead of Error. Here's an example of what it should look like below.

func getData(from url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void)
{
URLSession.shared.dataTask(with: url) { data, response, error
completion(data, response, error)
}.resume()
}

Weird Swift 3 issue with latest Amazon AWS Mobile sample code

In the last example which is presumably the proper syntax the closing parenthesis is missing.

You replaced

} as (Any?, Error?) -> Void)

with

} rather than expected })

I deleted everything which is not mandatory to make it clearer:

WSIdentityManager.defaultIdentityManager().resumeSession(completionHandler: { (result, error) in
print("Result: \(result) \n Error:\(error)")
})
isInitialized = true

or still shorter with trailing closure syntax:

WSIdentityManager.defaultIdentityManager().resumeSession() { (result, error) in
print("Result: \(result) \n Error:\(error)")
}
isInitialized = true

The type annotations (in completion blocks) are not needed. They could do more harm than good.

PS: Isn't isInitialized supposed to be in the completion block?



Related Topics



Leave a reply



Submit