2011-11-15 76 views
0

這是我的代碼,它嘗試使用NSURLDownload下載網頁。但它不起作用,它是一個命令行程序。使用NSURLDownload下載網頁

- (void)startDownloadingURL 
{ 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/index.html"] 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 

    // Create the download with the request and start loading the data. 
    NSURLDownload *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self]; 

    if (theDownload) { 
     // Set the destination file. 
     [theDownload setDestination:@"/saleh" allowOverwrite:YES]; 
    } else { 
     // inform the user that the download failed. 
     NSLog(@"download has failed!"); 
    } 

} 
+0

對此不起作用?它會打印「下載失敗!」嗎?您是否實施了任何[NSURLDownloadDelegate](http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLDownloadDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLDownloadDelegate)方法,例如' downloadDidFinish:'或'download:didFailWithError:'? –

+0

它不會給我任何錯誤!我已經實施了代表,但他們似乎不起作用!我不知道是什麼問題。 –

回答

0

確保您實施downloadDidFinish和doanload:didFailWithError:回調。

下面是介紹如何使用NSURLDownload概述:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-BAJEAIEE

具體來說,這個回調會給你失敗的原因的詳細信息:

- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error 
{ 
    // Release the connection. 
    [download release]; 

    // Inform the user. 
    NSLog(@"Download failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 

編輯:

下面你說這是一個命令行。異步回調需要一個NSRunLoop。請參閱:

Cocoa: NSURLConnection not attempting an HTTP Request

每文檔:

initWithRequest:委託: 返回一個初始化的URL下載的URL請求,並開始下載數據的請求。

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate 

delegate

The delegate for the download. This object will receive delegate messages as the download progresses. Delegate messages will be sent on the thread which calls this method. For the download to work correctly the calling thread’s run loop must be operating in the default run loop mode.

NSURLConnection的具有下載數據的同步方式。

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE

NSURLConnection provides support for downloading the contents of an NSURLRequest in a synchronous manner using the class method sendSynchronousRequest:returningResponse:error:. Using this method is not recommended, because it has severe limitations:

的主要限制是在客戶端的塊,但是這是在命令行應用程序的擔憂。

+0

這是一個命令行程序,是否會延遲工作? –

+0

我的程序是命令行,爲什麼不下載?因爲它不是一個GUI程序不能使用委託! –

+0

命令行應該不重要 - 創建一個執行main.m調用的工作(下載)的類。該類需要實現這些委託回調。 – bryanmac

0

您需要確保您至少定義了downloadDidFinish:download:didFailWithError:才能使用委託。您幾乎可以複製並粘貼URL Loading System Programming Guide中的方法。

如果你使用的是10.7,那麼在Lion沒有正式的協議之前,你應該聲明你在頭文件中使用了協議。

還要確保你有一個運行循環。對於測試,你可以在那裏粘貼[[NSRunLoop currentRunLoop] run];,但它可能不會退出循環。