我正在创建自己的协议并使用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

最佳答案

请参考NSNOtification Class
请在您想知道下载是否完成的类的ViewDidLoad方法中调用此方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finished:) name:@"requestfinishes" object:nil];


并称之为(void)didDownloadMap方法

[[NSNotificationCenter defaultCenter] postNotificationName:@"requestfinishes" object:nil userInfo:nil];

关于ios - 使用AFNetworking 2.0创建自己的委托(delegate)和协议(protocol),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20612737/

10-08 23:10