如何使 NSURLConnection 文件下载工作?

发布于 2024-11-04 05:46:05 字数 578 浏览 3 评论 0原文

我有一个 ViewController 声明为:

@interface DownloadViewController : UIViewController 
           <UITableViewDataSource, UITableViewDelegate>

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

我该如何结合它?我想使用 ViewController 方法,但无法使用 NSURLConnection

找到一个完整解释如何使用 NSURLConnection 下载文件的示例并不容易。每个人都只关注像didReceiveResponse这样简单的方法。

I have a ViewController declared as:

@interface DownloadViewController : UIViewController 
           <UITableViewDataSource, UITableViewDelegate>

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里寻她 2024-11-11 05:46:05

使用 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

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

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 !

毁梦 2024-11-11 05:46:05

如果您遇到问题,可以考虑使用备受推崇的 ASIHTTPRequest 库 来管理您的下载。它会为您照顾一切。

例如,只需 2 行即可完成。

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

If you're having problems, you could consider using the well regarded ASIHTTPRequest library to manage your download. It takes care of everything for you.

For example, just 2 lines will do it.

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:fullPathOfWhereToStoreFile];
调妓 2024-11-11 05:46:05

使用“NSURLConnection asynchronously”搜索该术语,您将找到源代码。或者只是 NSURLConnection。

例如:

用于异步 Web 服务调用的 NSURLConnection NSURLRequest 代理

<一个href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html" rel="nofollow noreferrer">通过示例代码使用 Apple 的 NSURLConnection

Objective-C 编程教程 – 创建 Twitter 客户端第 1 部分

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文