【问题标题】:How to make NSURLConnection file download work?如何使 NSURLConnection 文件下载工作?
【发布时间】:2011-04-27 11:55:53
【问题描述】:

我有一个 ViewController 声明为:

@interface DownloadViewController : UIViewController 
           <UITableViewDataSource, UITableViewDelegate>

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

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

要找到一个完整解释的示例如何使用 NSURLConnection 下载文件并不容易。大家只关注didReceiveResponse之类的简单方法。

【问题讨论】:

    标签: iphone download nsurlconnection nsobject


    【解决方案1】:

    使用 UIViewController 而不是 NSObject 应该不是您的问题! 我在 UIViewController 中使用 NSURLConnection 没有问题! 这是我的代码的一部分(不确定它会按原样编译):

    //
    //  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
    

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

    【讨论】:

    • 这段代码是用来下载文件的?我看不到你可以告诉它下载文件到哪个目录。
    • @MarcelMarino :文件存储在 NSMutableData *_responseData 对象中。正如我评论末尾所说,如果要将结果存储在文件中,则应将结果存储在文件中(此处未完成,但不难找到如何实现这一点;)
    【解决方案2】:

    如果您遇到问题,可以考虑使用广受好评的ASIHTTPRequest library 来管理您的下载。它会为您处理一切。

    例如,只需 2 行即可。

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadDestinationPath:fullPathOfWhereToStoreFile];
    

    【讨论】:

    • +1。自从我发现 ASIHTTPRequest 以来,我已经好几个月没碰 NSURLConnection 了。
    • 如果我单击 1 次下载 50 个文件,我只是调用 50 个请求?我现在不记得了,但是在检查这个 ASI 解决方案时我担心一些事情。
    • 我一次点击下载数百​​个(只在 WIFI 上提醒你),但我是按顺序执行的 - 即我将请求放入我自己的队列中,当每个请求完成时,我会开始下一个队列。我认为 ASIHTTPrequest 实际上可以为您完成所有这些工作,因为它也有排队功能。
    【解决方案3】:

    【讨论】:

    • 它并没有真正回答我的问题 ;) 前 2 个链接根本没有提到 ViewController,但也许我可以从这个 Twitter 客户端教程中学到一些东西..
    • @yosh:阅读第二个链接(Apple 的链接),他们以非常简单的方式解释了它,它不是 ViewController,你也可以将它与 UIView 一起使用,所有你需要有委托方法向您更新您的 NSURLConnection 请求进度的地方,因为第三个链接将对您有所帮助,谢谢
    • 我之前已经阅读过 Apple 链接,但我仍然对如何通过按一下按钮开始下载感到困惑。这就是我需要知道的,我会弄清楚关于保存文件和多文件下载的其余部分。检查 Twitter 解决方案,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    相关资源
    最近更新 更多