我正在创建自己的协议并使用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
答案 0 :(得分:3)
请参阅NSNOtification Class
。
请在您想知道下载是否已完成的类ViewDidLoad
方法中调用此方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finished:) name:@"requestfinishes" object:nil];
并将其称为(void)didDownloadMap
方法
[[NSNotificationCenter defaultCenter] postNotificationName:@"requestfinishes" object:nil userInfo:nil];