【发布时间】:2013-12-16 14:00:22
【问题描述】:
我正在使用 AFNetworking 2.0 创建自己的协议和委托。
我有一些方法可以显示下载进度(效果很好)、下载完成时间和下载开始时间。
问题是:我的下载完成后,我不知道如何在另一个班级知道。
有人有想法吗?建议?
非常感谢!
这是我的 .h 文件:
#import <Foundation/Foundation.h>
@class MapDownloader;
@protocol MapDownloaderDelegate <NSObject>
@optional
- (BOOL)mapDownloaderWillMapDownload:(MapDownloader *)downloader;
- (BOOL)mapDownloaderDidMapDownload:(MapDownloader *)downloader;
- (void)mapDownloader:(MapDownloader *)downloader progressDownloading:(float)progress;
@end
@interface MapDownloader : NSObject
@property (nonatomic, weak) id<MapDownloaderDelegate> delegate;
@property (nonatomic, strong) NSString *mapName;
- (void)downloadAsync:(NSString*)mapName;
@end
这是我的 .m 文件:
@implementation MapDownloader
- (void)downloadAsync:(NSString *)mapName
{
if (![self willDownloadMap])
return;
self.mapName = mapName;
[self startDownload];
}
#pragma mark -
#pragma mark Private Methods
- (void)startDownload
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL * URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kUrlWalk, self.mapName]];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSProgress *progress;
NSURLSessionDownloadTask *downloadTask =
[manager downloadTaskWithRequest:request
progress:&progress
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
return [self filePath];
}
completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
[self downloadComplete];
}];
[downloadTask resume];
[progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:nil];
}
- (void)downloadProgress:(double)progress
{
[self progressDownloading:progress];
}
- (void)downloadComplete
{
[self didDownloadMap];
}
#pragma mark Helpers
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSProgress *progress = (NSProgress *)object;
[self downloadProgress:progress.fractionCompleted];
}
- (NSURL*)documentPath
{
return [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
}
- (NSURL*)mapsPath
{
NSURL *documentsDirectoryPath = [self documentPath];
return [documentsDirectoryPath URLByAppendingPathComponent:kDirMap];
}
- (NSURL*)filePath
{
NSURL *mapsPath = [self mapsPath];
return [mapsPath URLByAppendingPathComponent:[NSString stringWithFormat:@"%@", self.mapName]];
}
#pragma mark - Events
- (BOOL)willDownloadMap
{
if ([self.delegate respondsToSelector:@selector(mapDownloaderWillMapDownload:)])
return [self.delegate mapDownloaderWillMapDownload:self];
return YES;
}
- (void)didDownloadMap {
if ([self.delegate respondsToSelector:@selector(mapDownloaderDidMapDownload:)])
[self.delegate mapDownloaderDidMapDownload:self];
}
- (void)progressDownloading:(float)progress {
if ([self.delegate respondsToSelector:@selector(mapDownloader:progressDownloading:)])
[self.delegate mapDownloader:self progressDownloading:progress];
}
@end
【问题讨论】:
-
您可以使用
delegate method或通知中心。 -
我该怎么做?因为下载完成后,“(void)didDownloadMap”方法被正确调用。但我不知道如何在我的应用程序的其他类中继续使用它:/
标签: ios iphone delegates protocols afnetworking-2