本文介绍了如何使NSURLConnection文件下载工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewController声明为:

I have a ViewController declared as:

@interface DownloadViewController : UIViewController 
           <UITableViewDataSource, UITableViewDelegate>

,我想使用 NSURLConnection 下载文件。 NSURLConnection 只是不启动,委托方法不起作用(例如, connection:didReceiveResponse 从未被调用)。我在一些示例代码中注意到该类是 NSObject 而不是 UIViewController 。

and I want to use NSURLConnection to download files. NSURLConnection simply "doesn't start", the delegate methods don't work (for example connection:didReceiveResponse is never called) . I noticed in some sample code that the class was subclassing NSObject instead of UIViewController.

如何组合?我想使用ViewController方法,但是我不能使用 NSURLConnection

How do I combine it? I want to use ViewController methods but then I can't use NSURLConnection.

找到一个完全解释的例子如何下载并不容易文件与NSURLConnection。每个人都只关注简单的方法,如 didReceiveResponse

It's not so easy to find a fully explained example how to download file with NSURLConnection. Everyone only concentrates on the easy methods like didReceiveResponse.

推荐答案

使用UIViewController而不是NSObject不是你的问题在这里!
我在UIViewController中使用NSURLConnection没有问题!
这是我的代码的一部分(不知道它会如何编译):

Using a UIViewController instead of an NSObject should not be your problem here !I'm using a NSURLConnection in an UIViewController with no issue !Here is a part of my code (not sure it will compile as it is) :

//
//  MyViewController.h
//

#import <Foundation/Foundation.h>

@interface MyViewController : UIViewController {
    @protected
    NSMutableURLRequest* req;
    NSMutableData* _responseData;
    NSURLConnection* nzbConnection;
}

- (void)loadFileAtURL:(NSURL *)url;

@end

-

//
//  MyViewController.m
//

#import "MyViewController.h"

@implementation MyViewController

- (void)loadView {  
// create your view here
}

- (void) dealloc {
    [_responseData release];

    [super dealloc];
}

#pragma mark -

- (void)loadFileAtURL:(NSURL *)url {
    // allocate data buffer
    _responseData = [[NSMutableData alloc] init];

    // create URLRequest
    req = [[NSMutableURLRequest alloc] init];
    [req setURL:_urlToHandle];

    nzbConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
    [req release];
    req = nil;
}


#pragma mark -

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Append data in the reception buffer
    if (connection == nzbConnection)
        [_responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (connection == nzbConnection) {
        [nzbConnection release];
        nzbConnection = nil;

        // Print received data
        NSLog(@"%@",_responseData);

        [_responseData release];
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // Something went wrong ...
    if (connection == nzbConnection) {
        [nzbConnection release];
        [_responseData release];
    }
}

@end

如果您计划下载大型文件,请考虑将接收的数据包存储在文件中,而不是将其存储在内存中!

If you plan to download large files, consider storing the received packets in a file instead of storing it in memory !

这篇关于如何使NSURLConnection文件下载工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:50