2012-03-12 15 views
0

私は、ファイルをダウンロードする際、私は、接続を停止したいので、私が作成した、これは私が使用したコードである 、サーバからデータを受信するnsurlconnectionを停止する方法は?

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response { 

filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Ep1.mp4"; 

[[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil]; 
file =[NSFileHandle fileHandleForUpdatingAtPath:filename] ; 

if (file) { 

    [file seekToEndOfFile]; 
}} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 


if (file) { 

    [file seekToEndOfFile]; 

} [file writeData:data]; } 


- (void)connectionDidFinishLoading:(NSURLConnection*)connection { 


[file closeFile]; 
} 


-(void)downloadFile{ 

targetURL = [NSURL URLWithString:@"http://some url "]; 

DownloadRequest = [NSURLRequest requestWithURL:targetURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:50.0]; 
DownloadConnection = [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self]; 

if (DownloadConnection) { 
    receivedData = [NSMutableData data]; 
} 
} 

をnsfileハンドル付きURL接続を使用していますボタン --->

- (void)buttonPressed:(id)sender 
{ 
DownloadConnection = [[NSURLConnection alloc] initWithRequest: DownloadRequest delegate:self]; 
[ DownloadConnection cancel]; 
} 

が、動作しません、データがまだ受信、私は何かを理解して欠場しましたか? または何ができますか?

+0

参照[この](http://stackoverflow.com/a/7229304/845115) –

+0

ありがとう〜私は "自己"が恋しい.... – cmin

答えて

4

編集: これは私が見つけた解決策である:

- (void)buttonPressed:(id)sender 
{ 
DownloadConnection = [[NSURLConnection alloc] initWithRequest: DownloadRequest delegate:self]; 
[ self.DownloadConnection cancel]; 
} 

は、現在の接続を解除するために自己を追加...

関連する問題