2016-11-23 24 views
0

我有一個iOS應用程序,我從一個亞馬遜S3服務器的plist文件中獲取數據。我想使用新的NSURLSession框架來獲取數據。所以我有以下代碼(我使用的是Ray Wenderlich網絡教程)。NSURLSession沒有從plist文件檢索數據

NSString *dataUrl = @"https://s3.amazonaws.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.plist"; 
    NSURL *url = [NSURL URLWithString:dataUrl]; 

    // 2 
    NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession] 
              dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
               // 3: Handle response here with more of my code 
              }];// 4  
[downloadPhotoTask resume]; 

如果我從* dataURL複製URL並將其放到網絡瀏覽器中,它就會下載文件。但在我的iOS應用程序中沒有數據,錯誤是「流中的字節太少」。

有沒有人有我缺少的想法?

+1

你正在恢復錯誤的任務。 – vadian

+0

因此,在你的completionHandler(評論#3)數據是零,你會得到一個NSError,說流中的字節太少?你能發佈確切的錯誤消息,以及關於'data'和'response'的信息嗎? –

+0

您是否檢查了completionHandler中的錯誤?此外,您的代碼與Ray Wenderlich代碼不同,具體而言,downloadTask是一個局部變量。我不知道它在超出範圍時的表現如何,但很有可能會取消下載。 – Gruntcakes

回答

0

從第一次看代碼本身應該工作。你有沒有想過 [downloadTask resume];

+0

是的,它是[downloadTask resume]。我的錯誤 – K2Digital

+0

原來我引用了錯誤的數據流。我的錯。謝謝大家的支持。 – K2Digital

+0

這個[link](https://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift)在服務器端使用plist文件。 – Nik