Xcode 如何停止连接?
我使用带有nsfile句柄的url连接从服务器接收数据, 这是我使用的代码Xcode 如何停止连接?,xcode,download,nsurlconnection,Xcode,Download,Nsurlconnection,我使用带有nsfile句柄的url连接从服务器接收数据, 这是我使用的代码 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response { filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] string
- (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];
}
}
我想在下载文件时停止连接,所以,我创建了按钮
--->
但是不起作用,数据还在接收,我是不是错过了什么?
或者我能做什么?编辑:
这就是我找到的解决方案:
- (void)buttonPressed:(id)sender
{
DownloadConnection = [[NSURLConnection alloc] initWithRequest: DownloadRequest delegate:self];
[ self.DownloadConnection cancel];
}
添加self以取消当前连接…编辑:
这就是我找到的解决方案:
- (void)buttonPressed:(id)sender
{
DownloadConnection = [[NSURLConnection alloc] initWithRequest: DownloadRequest delegate:self];
[ self.DownloadConnection cancel];
}
添加self以取消当前连接…请参阅“谢谢”~我错过了“self”…哈哈谢谢~我错过了“self”…哈哈