首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SDK4.2中未检测到NSURLDownload

SDK4.2中未检测到NSURLDownload
EN

Stack Overflow用户
提问于 2010-12-15 21:01:37
回答 2查看 1.3K关注 0票数 0

伙计们,这是我的代码:

代码语言:javascript
复制
#import <Foundation/Foundation.h>
@interface Updater : NSObject {

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

}

@end

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

代码语言:javascript
复制
Updater.h:15: error: expected specifier-qualifier-list before 'NSURLDownload'

你知道为什么我会收到这个错误消息吗?我已经包括了基础框架,我很难理解为什么编译器会抱怨,特别是考虑到它根本没有抱怨NSURLConnection。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-15 21:07:04

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

iOS注意:NSURLDownload类在iOS中不可用,因为不鼓励直接下载到文件系统。请改用NSURLConnection类

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

票数 2
EN

Stack Overflow用户

发布于 2011-09-11 16:11:55

正如你在iOS中所读到的:现在还不能在ios上使用NSURLDownload,但是下面提供的链接向你展示了一种简单的方法,可以从网上下载任何你想要的东西到应用程序沙箱中。希望你会喜欢。干杯http://www.ifans.com/forums/showthread.php?t=169611

-下面是链接

代码语言:javascript
复制
//
//  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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4450140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档