2013-05-06 41 views
0

我創建了一個具有一個頁面的應用程序,並且在該頁面中存在一個按鈕,該按鈕在點擊時下載了文件。在xcode中使用AFNetworking進行下載文件時出現錯誤

我可以下載文件,AFNetworking但我有錯誤....

這是我的代碼:

- (IBAction)downloads:(id)sender 
{ 
    NSLog(@"start downloads"); 
    NSURLRequest *request = [NSURLRequest requestWithURL: 
    [NSURL URLWithString: 
    @"http://.../500x333xgalaxy-core-product-image-1tn.jpg, 
          q1367836642.pagespeed.ic.eafBYU8jUN.jpg"]]; 
    AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] 
                  initWithRequest:request]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                    NSUserDomainMask, YES); 
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent 
                     :@"filename.jpg"]; 

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, 
    id responseObject) { 
     NSLog(@"Successfully downloaded file to %@", path); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 

} 

enter image description here

此錯誤是:

No visible @interface 'AFURLConnectionOpretion' declares the selector 
                 'setCompletionBlockWithSuccess' 
+0

「AFURLConnectionOperation *操作」 更改此到「AFHTTPRequestOperation *操作」 – 2013-05-06 13:21:37

+0

我的朋友正確它,但我不從網址下載文件!爲什麼? – emma 2013-05-06 13:28:16

回答

0

使用下面的行開始操作:

[operation start]; 
0

這是我的下載代碼

AFHTTPRequestOperation *urlConnection; 
urlConnection = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
urlConnection.outputStream = [NSOutputStream outputStreamToFileAtPath:outputPath append:NO]; 
[urlConnection setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSLog(@"Download Completed"); 


} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

    NSLog(@"Error occured while downloading"); 

}]; 

[urlConnection setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 

}]; 

變化 「AFURLConnectionOperation *操作」 到 「AFHTTPRequestOperation *操作」 我認爲是問題

+0

我的朋友吧,但我不從網址下載文件!爲什麼? – emma 2013-05-06 13:25:56

+0

你的意思是還沒有下載? – 2013-05-06 13:47:54

+0

沒有我的朋友爲什麼? – emma 2013-05-06 17:24:47

相關問題