NSURLSession completion block not called - macos

var session = NSURLSession.sharedSession()
session.dataTaskWithRequest(urlRequest,
completionHandler: {(data: NSData!,
response: NSURLResponse!,
error: NSError!) in
println(data)
println(response)
println(error)
})
So I am making this request, and the completion block is never called.
What's wrong?
Also I tried a synchronous and asynchronous form of the same request with NSURLConnection and it worked perfectly.
EDIT:
I tried assigning a dataTask variable to the session.dataTaskWithRequest and displayed it right after. It says this <__NSCFLocalDataTask: 0xb41bda0> { suspended } Suspended? Why?

So I tried calling it like this
session.dataTaskWithRequest(urlRequest,
completionHandler: {(data: NSData!,
response: NSURLResponse!,
error: NSError!) in
print(data)
print(response)
print(error)
}).resume()
And it worked.
Seems like I have to call resume() on a default suspended session task.

Are you using playgrounds??
If you are, you should be careful to include:
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
In order to make the playground wait for the callback

I face the same problem and I solved it by
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
if (!error)
{
NSLog(#"Data is %#",data);
NSLog(#"Response is %#",response);
NSLog(#"Error is %#",error);
}
}];
[dataTask resume];
And check that you are added the App Transport Security Settings in your info.plist.

You can also use it simply by :-
let url = "api url"
let nsURL = NSURL
let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL) {
(data, response, error) in
// your condition on success and failure
}
task.resume()

This is a fairly unique case, but if you're using a URLSession inside a unit test, you'll need to setup the expectations. Otherwise, your test case will end and it will appear that your request is never returning. Swift 3.0.1.
let expect = expectation(description: "dataTaskWithRequest - completes")
if let url = URL(string: "https://www.google.com/") {
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { ( data, response, error) in
print(data.debugDescription)
print(response.debugDescription)
print(error.debugDescription)
expect.fulfill()
}.resume()
waitForExpectations(timeout: 10, handler: nil)
}

It'll be something like this in Swift 2.x
NSURLSession.sharedSession().dataTaskWithRequest(theRequest) { (data, response , error) in
print(response)
}.resume()

Related

How to send some count of POST/GET requests simultaneously?

I'm learning swift, trying to send 2 and more requests not one by one, but simultaneously. Is it possible with NSURLSession?
NSURLSession is asynchronous which means it is sent on a different thread and can be run multiple at once.
This link explains and gives an example on how to handle the response back on the main thread etc:
https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started
func sendRequest() {
let defaultSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
var dataTask: NSURLSessionDataTask?
let url = NSURL(string: "http://www.url.com")
dataTask = defaultSession.dataTaskWithURL(url!) {
data, response, error in
dispatch_async(dispatch_get_main_queue()) {
//Handle response
if let error = error {
//Error - handle 'error'
print(error.localizedDescription)
} else if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
//Success - handle 'data'
}
}
}
}
dataTask?.resume()
}
Hope this helps

FBSDKGraphRequest cannot post image and text anymore (no more text)

I'm posting to a group user wall, (image and text). It have worked from a long time, and stop working recently(now, image are posted, but no text :( ). Any idea if there was a new way or rules? (no error reported)
[params setObject:sContent forKey:#"message"];
[params setObject:yourImageData forKey:#"picture"];
[[[FBSDKGraphRequest alloc]
initWithGraphPath:#"xxxxxxxxxxxxxxx/photos"
parameters: params
HTTPMethod:#"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
Following is the working code for me(In Swift):
let params = ["message": "Test message", "sourceImage" : UIImagePNGRepresentation(UIImage(named: "test.jpg")!)!]
FBSDKGraphRequest(graphPath: "me/photos", parameters: params, HTTPMethod: "POST").startWithCompletionHandler({ (connection, result, error) -> Void in
guard let response = result else {
print("No response received")
if let errorInfo = error {
print("errorInfo: \(errorInfo)")
}
return }
print(response)
})
Check 'Publishing' section at the link for more.
Let me know if you have any difficulties to convert it to Objective-C.

Swift 2.0 NSURLConnection sendSynchronousRequest

I am using the code below to check for an internet connection. It was working correctly but after the Swift 2.0 update I now see an error on the line var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData? saying extra argument 'error' in call.
class func isConnectedToNetwork()->Bool{
var Status:Bool = false
let url = NSURL(string: "http://google.com/")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "HEAD"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10.0
var response: NSURLResponse?
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
Status = true
}
}
return Status
}
Do you have any ideas what I should change it to?
Thanks
If you look at apples documentation (https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/#//apple_ref/occ/clm/NSURLConnection/sendSynchronousRequest:returningResponse:error:) you'll see that the definition changed to this:
class func sendSynchronousRequest(_ request: NSURLRequest,
returningResponse response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>) throws -> NSData
They have removed the error parameter and the method throws now an ErrorType, if the request fails. So this should work:
do {
let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)
} catch (let e) {
print(e)
}
However you shouldn't use this method: It's deprecated in favor of NSURLSession since iOS 9 and OS X 10.11.

Swift URL Response is nil

I have created a custom DataManager class. Inside it I want to fetch data in a method and return an NSData object to convert to JSON afterwards.
I have tried to get the data using the completionHandler but no luck:
class func fetchData() -> NSData? {
var session = NSURLSession.sharedSession(),
result = NSData?()
let DataURL : NSURL = NSURL(string: "http://...file.json")!
let sessionTask = session.dataTaskWithURL(DataURL, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
result = data
})
sessionTask.resume()
return result
}
The dataTask runs asynchronously. That means that the completion handler closure will not be called by the time you return from fetchData. Thus, result will not have been set yet.
Because of this, you should not try to retrieve data synchronously from an asynchronous method. Instead, you should employ an asynchronous completion handler pattern yourself:
class func fetchData(completion: #escaping (Data?, Error?) -> Void) {
let session = URLSession.shared
let url = URL(string: "http://...file.json")!
let task = session.dataTask(with: url) { data, response, error in
completion(data, error)
}
task.resume()
}
And you'd call it like so:
MyClass.fetchData { data, error in
guard let data = data, error == nil else {
print(error ?? "Unknown error")
return
}
// use `data` here; remember to dispatch UI and model updates to the main queue
}
// but do not try to use `data` here
...
FYI, for the original pre-Swift 3 syntax, see previous revision of this answer.

XCode 6 UILabel not refreshing text when calling from complition handler

I'm calling to refresh label after refreshQuote() function finishes. When look at the log to see what happened with label.text I can see it has been replaced however the view still looks the same.
Im calling refreshQuote() from viewDidload.
When calling setQuoteLabel directly from viewDidLoad it all works fine
Please help, loosing hope now.
Thanks in advance
func refreshQuote()
{
var url : String = "https://api.parse.com/1/classes/Quotes"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.addValue("------", forHTTPHeaderField: "X-Parse-Application-Id")
request.addValue("------strong text", forHTTPHeaderField: "X-Parse-REST-API-Key")
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
NSLog(error.debugDescription);
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if (jsonResult != nil)
{
var allResults: NSArray = jsonResult["results"] as NSArray
var pickedResult: NSDictionary = allResults[0] as NSDictionary
let q:String = pickedResult["message"] as String!
self.setQuoteLabel(q);
} else
{
// couldn't load JSON, look at error
}
})
}
func setQuoteLabel (quote:String)
{
NSLog(quote)
quoteLabel.text = quote;
quoteLabel.setNeedsDisplay();
NSLog(quoteLabel.text!)
}
All UI components must be updated from the main thread, whereas in your case you are updating most likely from a different thread, because the code is in an asynchronous block.
To force a block of code to be executed in the main thread, you can use dispatch_async as follows:
dispatch_async (dispatch_get_main_queue ()) {
self.setQuoteLabel(q)
}

Resources