SDK 4.2中未检测到NSURLDownload

时间:2010-12-15 13:01:37

标签: ios ios4 nsurlconnection

Ey伙计们,这是我的代码:

#import <Foundation/Foundation.h>
@interface Updater : NSObject {

    NSURLConnection *updateConnection;
    NSURLDownload *downloadConnection; //error at this line

}

@end

这是我在标记线上得到的错误:

Updater.h:15: error: expected specifier-qualifier-list before 'NSURLDownload'

知道我收到此错误消息的原因吗?我已经包含了基础框架,并且难以理解编译器为什么抱怨,特别是考虑到它根本没有抱怨NSURLConnection。谢谢!

2 个答案:

答案 0 :(得分:2)

NSURLDownload类仅适用于MacOS,在iOS上您应使用NSURLConnection。来自docs

  

iOS注意:NSURLDownload类是   在iOS中不可用,因为   直接下载到文件   不鼓励制度。使用   而是NSURLConnection类

如果要下载大量数据并希望避免内存问题,可以在连接委托中使用NSFileHandle类将接收到的数据直接写入磁盘,而不是将其保留在内存中。

答案 1 :(得分:1)

正如您在iOS中所读到的那样:在ios上无法使用NSURLDownload,但下面提供的链接向您显示了一种从网络下载任何内容到应用程序沙箱的简单方法。希望你喜欢。 干杯 http://www.ifans.com/forums/showthread.php?t=169611

------------下面是链接------------

//
//  UIDownloadBar.h
//  UIDownloadBar
//
//  Created by John on 3/20/09.
//  Copyright 2009 Gojohnnyboi. All rights reserved.
//

#import <UIKit/UIKit.h>

@class UIProgressView;
@protocol UIDownloadBarDelegate;

@interface UIDownloadBar : UIProgressView {
    NSURLRequest* DownloadRequest;
    NSURLConnection* DownloadConnection;
    NSMutableData* receivedData;
    NSString* localFilename;
    id<UIDownloadBarDelegate> delegate;
    long long bytesReceived;
    long long expectedBytes;

    float percentComplete;
}

- (UIDownloadBar *)initWithURL:(NSURL *)fileURL progressBarFrame:(CGRect)frame timeout:(NSInteger)timeout delegate:(id<UIDownloadBarDelegate>)theDelegate;

@property (nonatomic, readonly) NSMutableData* receivedData;
@property (nonatomic, readonly, retain) NSURLRequest* DownloadRequest;
@property (nonatomic, readonly, retain) NSURLConnection* DownloadConnection;
@property (nonatomic, assign) id<UIDownloadBarDelegate> delegate;

@property (nonatomic, readonly) float percentComplete;

@end

@protocol UIDownloadBarDelegate<NSObject>

@optional
- (void)downloadBar:(UIDownloadBar *)downloadBar didFinishWithData:(NSData *)fileData suggestedFilename:(NSString *)filename;
- (void)downloadBar:(UIDownloadBar *)downloadBar didFailWithError:(NSError *)error;
- (void)downloadBarUpdated:(UIDownloadBar *)downloadBar;

@end


//
//  UIDownloadBar.m
//  UIDownloadBar
//
//  Created by John on 3/20/09.
//  Copyright 2009 Gojohnnyboi. All rights reserved.
//

#import "UIDownloadBar.h"


@implementation UIDownloadBar

@synthesize DownloadRequest, 
    DownloadConnection,
    receivedData,
    delegate,
    percentComplete;


- (UIDownloadBar *)initWithURL:(NSURL *)fileURL progressBarFrame:(CGRect)frame timeout:(NSInteger)timeout delegate:(id<UIDownloadBarDelegate>)theDelegate {
    self = [super initWithFrame:frame];
    if(self) {
        self.delegate = theDelegate;
        bytesReceived = percentComplete = 0;
        localFilename = [[[fileURL absoluteString] lastPathComponent] copy];
        receivedData = [[NSMutableData alloc] initWithLength:0];
        self.progress = 0.0;
        self.backgroundColor = [UIColor clearColor];
        DownloadRequest = [[NSURLRequest alloc] initWithURL:fileURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeout];
        DownloadConnection = [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self startImmediately:YES];

        if(DownloadConnection == nil) {
            [self.delegate downloadBar:self didFailWithError:[NSError errorWithDomain:@"UIDownloadBar Error" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"NSURLConnection Failed", NSLocalizedDescriptionKey, nil]]];
        }
    }

    return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.receivedData appendData:data];

    NSInteger receivedLen = [data length];
    bytesReceived = (bytesReceived + receivedLen);

    if(expectedBytes != NSURLResponseUnknownLength) {
        self.progress = ((bytesReceived/(float)expectedBytes)*100)/100;
        percentComplete = self.progress*100;
    }

    [delegate downloadBarUpdated:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self.delegate downloadBar:self didFailWithError:error];
    [connection release];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    expectedBytes = [response expectedContentLength];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.delegate downloadBar:self didFinishWithData:self.receivedData suggestedFilename:localFilename];
    [connection release];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}

- (void)dealloc {
    [localFilename release];
    [receivedData release];
    [DownloadRequest release];
    [DownloadConnection release];
    [super dealloc];
}


@end