2011-06-24 6 views
1

インターネットからファイルをダウンロードすることは可能ですか?私はリンゴの開発者のサイトでいくつかの文書を調べました。 NSURLDownloadを使用することは可能だが、iOSでは使用できないという。したがって、iOSの場合にのみNSURLConnectionを使用して行う必要があります。だから、誰かが私が特定のURLからファイル(HTMLページさえ)をダウンロードする手助けをします。前もって感謝します。URLが指定されているときにファイルをダウンロードする - iPhone sdk

答えて

4

からここでは、サーバから画像を取得し、それをファイルに保存する例を示します。

今すぐデータに

を保存し、終了したデータ

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [self.activeDownload appendData:data]; 
} 

転送を受信して​​接続

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
NSURL *url = [[NSURL alloc] initWithString:@"URL TO FILE"]; 
[request setURL:url]; 
[url release]; 
url = nil; 

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

ハンドルを作成し、あなたのインスタンス変数

NSMutableData *activeDownload; 
NSURLConnection *imageConnection; 

を作成します。 10

私が表示していないコードの他のビット(プロパティなど)がありますが、これで十分に助けてくれるはずです。

+1

私はこのコードを使用してうまくいきますが、言わなければならないことがいくつかあると思います。経験が少ない人はそれを動作させることができません:1)NSURLConnection * connはインスタンス変数またはXcode変数が使用されていないので警告が表示されます... 2)ダウンロードを開始するときに、activeDownload変数(self.activeDownload = [[NSMutableData alloc] init;)を初期化する必要があります。 – Blue

1

私はASIHTTPRequestライブラリはあなたを助けになると思う:)

ASIWebPageRequest

ASIHTTPRequestに含まれてASIWebPageRequestクラスを使用すると、画像やスタイルシートなどの外部リソースを含む完全なWebページを、ダウンロードすることができます。

コード例allseeing-i.com

- (IBAction)loadURL:(NSURL *)url 
{ 
    // Assume request is a property of our controller 
    // First, we'll cancel any in-progress page load 
    [[self request] setDelegate:nil]; 
    [[self request] cancel]; 

    [self setRequest:[ASIWebPageRequest requestWithURL:url]]; 
    [[self request] setDelegate:self]; 
    [[self request] setDidFailSelector:@selector(webPageFetchFailed:)]; 
    [[self request] setDidFinishSelector:@selector(webPageFetchSucceeded:)]; 

    // Tell the request to embed external resources directly in the page 
    [[self request] setUrlReplacementMode:ASIReplaceExternalResourcesWithData]; 

    // It is strongly recommended you use a download cache with ASIWebPageRequest 
    // When using a cache, external resources are automatically stored in the cache 
    // and can be pulled from the cache on subsequent page loads 
    [[self request] setDownloadCache:[ASIDownloadCache sharedCache]]; 

    // Ask the download cache for a place to store the cached data 
    // This is the most efficient way for an ASIWebPageRequest to store a web page 
    [[self request] setDownloadDestinationPath: 
     [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self request]]]; 

    [[self request] startAsynchronous]; 
} 

- (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest 
{ 
    // Obviously you should handle the error properly... 
    NSLog(@"%@",[theRequest error]); 
} 

- (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest 
{ 
    NSString *response = [NSString stringWithContentsOfFile: 
     [theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil]; 
    // Note we're setting the baseURL to the url of the page we downloaded. This is important! 
    [webView loadHTMLString:response baseURL:[request url]]; 
} 
+0

これはウェブページ全体をダウンロードするためのものです。元の質問は「ファイル」のダウンロードを求めていました。このライブラリで任意のファイルをダウンロードすることは可能ですか? –

+0

私は誤解しましたが、図書館はどんなサイズの通常のファイルもダウンロードできます。 – Mario

+0

このライブラリが存在することは興味深いことです。将来的には役に立つかもしれない!情報をありがとう。 –

0

はいNSURLConnectionで行うことができます。 URLをリクエストする必要があります。このようなもの。

NSString *strDownloadURL = @"File URL"; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:strDownloadURL]]; 

Connection *con = [[[Connection alloc] init]; 
con.delegate=self; 
[[[NSURLConnection alloc]initWithRequest:request delegate:con] autorelease]; 

これで、接続代理メソッドdidReceiveDataで、グローバル変数にNSDataを割り当てます。

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data { 
    if (receivedData != nil) { 
    [receivedData appendData:data]; 
    } else { 
    receivedData = [[NSMutableData alloc] initWithData:data]; 
    } 
} 

最後に、接続代理メソッド「connectionDidFinishLoading」ファイルデータをNSData形式にする必要があります。したがって、Documentディレクトリにこのようなものを格納します。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    [receivedData writeToFile:DocumentPathtoSaveFile atomically:YES]; 
} 
+0

Connectionとは何ですか? – iOS

+0

申し訳ありませんが、自分の個人的な使用のためにConnection Customクラスを作成しました。あなたは単に "Connection * con = [[[Connection alloc] init]; con.delegate = self; [[[NSURLConnection alloc] initWithRequest:delegate:con] autorelease];"これに "NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:要求デリゲート:自己];" – Deeps

関連する問題