Iphone 如何强制关闭连接?

Iphone 如何强制关闭连接?,iphone,xcode,cocoa,Iphone,Xcode,Cocoa,我正在使用NSURLConnection进行http下载。根据具体情况,我可能不得不在特定下载后断开连接。比如说,如果我的下载在30秒内完成,那么ok other wise下载应该在30秒后停止。我还必须记录这些事件。我的问题是,即使在30秒后,它仍会继续下载数据,并且只有在下载完成后才会记录事件 简单地说,我想强制关闭下载,还想禁用http连接触发的所有委托事件。我不想在多个位置设置标志,这会使事情进一步复杂化。有什么想法吗 更新:完整场景: 我已在NSURREQUEST对象中将timeout

我正在使用NSURLConnection进行http下载。根据具体情况,我可能不得不在特定下载后断开连接。比如说,如果我的下载在30秒内完成,那么ok other wise下载应该在30秒后停止。我还必须记录这些事件。我的问题是,即使在30秒后,它仍会继续下载数据,并且只有在下载完成后才会记录事件

简单地说,我想强制关闭下载,还想禁用http连接触发的所有委托事件。我不想在多个位置设置标志,这会使事情进一步复杂化。有什么想法吗

更新:完整场景: 我已在NSURREQUEST对象中将timeoutInterval设置为10秒。现在,如果10秒钟内没有收到数据,会发生什么情况?10秒钟后,连接会自动断开,工作正常。但我的软件中还有一个功能,需要在给定的时间内终止与下载的连接。我正在使用一个单独的NSTimer。我所能做的就是在触发NSTimer事件时设置一个标志。现在,如果标志是通过NSTimer设置的,并且数据停止进入,我没有在接下来的10秒钟内触发的连接委托。现在我的问题是中止事件和超时事件同时发生。

那么,您可以通过向NSURLConnection发送取消事件来“取消”NSURLConnection:

[connection cancel];
看看苹果

在此之前,只需取消代理,您不应受到任何代理回调的骚扰。

那么,您可以通过向NSURLConnection发送取消事件来“取消”NSURLConnection:

[connection cancel];
看看苹果


在此之前,只需取消代理,您不应受到任何代理回调的骚扰。

使用
NSURLRequest
对象为您使用此方法下载的evrey请求指定超时时间

请检查您的
NSURLConnection
的委托是否已设置并响应该方法。
NSURLConnection
在连接完成时调用此方法或
connectiondFinishLoading:

处理“didFailWithError”方法,并使用
NSError
对象检查故障原因

但如果您从服务器得到响应,并且响应时间很慢,则使用
NSTimer
。 创建用于下载数据的帮助器类,这样您可以通过创建多个实例并在其中设置NSTimer(如果下载在30秒内完成)来重复使用该类进行多次下载,从而使计时器无效,否则取消下载
[self.connection cancel]

请检查以下代码:

- (void)_startReceive
    // Starts a connection to download the current URL.
{
    BOOL                success;
    NSURL *             url;
    NSURLRequest *      request;

        // Open a connection for the URL.
        request = [NSURLRequest requestWithURL:url];
        assert(request != nil);

        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
        assert(self.connection != nil);

        // If we've been told to use an early timeout for get complete response within 30 sec, 
        // set that up now.
            self.earlyTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(_earlyTimeout:) userInfo:nil repeats:NO];
    }
}

- (void)_stopReceiveWithStatus:(NSString *)statusString
    // Shuts down the connection and displays the result (statusString == nil) 
    // or the error status (otherwise).
{
    if (self.earlyTimeoutTimer != nil) {
        [self.earlyTimeoutTimer invalidate];
        self.earlyTimeoutTimer = nil;
    }
    if (self.connection != nil) {
        [self.connection cancel];
        self.connection = nil;
    }
}

- (void)_earlyTimeout:(NSTimer *)timer
    // Called by a timer (if the download is not finish)
{
    [self _stopReceiveWithStatus:@"Early Timeout"];
}

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
    // A delegate method called by the NSURLConnection when the request/response 
    // exchange is complete.  
{ }

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
    // A delegate method called by the NSURLConnection as data arrives.  We just 
    // write the data to the file.
{ }

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
    // A delegate method called by the NSURLConnection if the connection fails. 
{
    NSLog(@"didFailWithError %@", error);   
    // stop Receive With Status Connection failed
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
    // A delegate method called by the NSURLConnection when the connection has been 
    // done successfully.  We shut down the connection with a nil status.
{
    NSLog(@"connectionDidFinishLoading");
    // If control reach here before timer invalidate then save the data and invalidate the timer
     if (self.earlyTimeoutTimer != nil) {
        [self.earlyTimeoutTimer invalidate];
        self.earlyTimeoutTimer = nil;
    }
}

使用
NSURLRequest
对象为使用此方法下载的evrey请求指定超时时间

请检查您的
NSURLConnection
的委托是否已设置并响应该方法。
NSURLConnection
在连接完成时调用此方法或
connectiondFinishLoading:

处理“didFailWithError”方法,并使用
NSError
对象检查故障原因

但如果您从服务器得到响应,并且响应时间很慢,则使用
NSTimer
。 创建用于下载数据的帮助器类,这样您可以通过创建多个实例并在其中设置NSTimer(如果下载在30秒内完成)来重复使用该类进行多次下载,从而使计时器无效,否则取消下载
[self.connection cancel]

请检查以下代码:

- (void)_startReceive
    // Starts a connection to download the current URL.
{
    BOOL                success;
    NSURL *             url;
    NSURLRequest *      request;

        // Open a connection for the URL.
        request = [NSURLRequest requestWithURL:url];
        assert(request != nil);

        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
        assert(self.connection != nil);

        // If we've been told to use an early timeout for get complete response within 30 sec, 
        // set that up now.
            self.earlyTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(_earlyTimeout:) userInfo:nil repeats:NO];
    }
}

- (void)_stopReceiveWithStatus:(NSString *)statusString
    // Shuts down the connection and displays the result (statusString == nil) 
    // or the error status (otherwise).
{
    if (self.earlyTimeoutTimer != nil) {
        [self.earlyTimeoutTimer invalidate];
        self.earlyTimeoutTimer = nil;
    }
    if (self.connection != nil) {
        [self.connection cancel];
        self.connection = nil;
    }
}

- (void)_earlyTimeout:(NSTimer *)timer
    // Called by a timer (if the download is not finish)
{
    [self _stopReceiveWithStatus:@"Early Timeout"];
}

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
    // A delegate method called by the NSURLConnection when the request/response 
    // exchange is complete.  
{ }

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
    // A delegate method called by the NSURLConnection as data arrives.  We just 
    // write the data to the file.
{ }

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
    // A delegate method called by the NSURLConnection if the connection fails. 
{
    NSLog(@"didFailWithError %@", error);   
    // stop Receive With Status Connection failed
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
    // A delegate method called by the NSURLConnection when the connection has been 
    // done successfully.  We shut down the connection with a nil status.
{
    NSLog(@"connectionDidFinishLoading");
    // If control reach here before timer invalidate then save the data and invalidate the timer
     if (self.earlyTimeoutTimer != nil) {
        [self.earlyTimeoutTimer invalidate];
        self.earlyTimeoutTimer = nil;
    }
}

糟糕的是,我忘了提到我正在使用timeoutINterval。我请求的Timeoutinterval是一个常量,为10秒。我还有一个30秒的计时器。如果下载处于活动状态,TimeoutInterval也不会强制停止。我想强制关闭,即使下载进展缓慢。我的坏消息是我忘了提到我正在使用timeoutINterval。我请求的Timeoutinterval是一个常量,为10秒。我还有一个30秒的计时器。如果下载处于活动状态,TimeoutInterval也不会强制停止。我想强制关闭,即使下载进展缓慢。