2015-10-02 76 views
0

任何想法爲什麼NSURLSession完成塊不被調用?隨着NSURLConnection它的工作原理,但NSURLConnection在iOS版9NSURLSession不返回

let username = "8r6nibK8Pe28WFhjTX1gGb1Rhhhc7xM02v7XKRPedL4" 
let password = "8r6nibK8Pe28WFhjTX1gGb1Rhhhc7xM02v7XKRPedL4" 
let loginString = NSString(format: "%@:%@", username, password) 
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)! 
let base64LoginString = loginData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) 

// create the request 
let url = NSURL(string: "https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query='Rambo'+'Oscar'") 
let request = NSMutableURLRequest(URL: url!) 
request.HTTPMethod = "POST" 
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization") 

let a = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()).dataTaskWithRequest(request, completionHandler: {data, urlResponse, error in 

    if data != nil { 

     let str = NSString(data: data!, encoding: NSUTF8StringEncoding) 
     print(str) 
    } else { 
     _ = 7 
    } 
}) 
+0

更新回答給答案。 – Abizern

回答

1

您的變量a是NSURLSessionDataTask棄用。

但你只是在創造它。一旦你有它,你需要實際運行它:

a.resume() 
+0

順便說一句,你知道爲什麼打印的信息包含:'你提供的授權類型不支持。只支持Basic和OAuth',我正在使用基本身份驗證。我希望對圖像信息有一些有意義的迴應。 –

+0

嘗試將授權添加到配置,而不是請求。例如:https://gist.github.com/n8armstrong/5c5c828f1b82b0315e24 – Abizern

+0

嘗試過,不起作用,但創建了另一個問題:http://stackoverflow.com/questions/32907981/nsurlsession-returns-different-response-than -browser-會,爲什麼 –

相關問題