ios - 使用 AFNetworking 2.0 创建自己的委托(delegate)和协议(protocol)

标签 ios iphone delegates protocols afnetworking-2

我正在使用 AFNetworking 2.0 创建自己的协议(protocol)和委托(delegate)。

我有一些方法可以显示下载进度(效果很好)、下载完成时间和下载开始时间。

问题是:我不知道如何在另一个类(class)知道我的下载何时完成。

有人有想法吗?建议?

非常感谢!

这是我的 .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类。 请在你想知道下载是否完成的类的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/

相关文章:

ios - 在 UIImageView 上使用 Tint 颜色

ios - 如何在 UITableView Header 上添加 UICollectionView

iOS 应用内购买和销售应用内货币

iphone - 双击以全屏显示

java - Java 8 中有委托(delegate)吗?

c++ - 使用委托(delegate)调用 C++ 函数时 VS2015 应用程序崩溃

ios - Paypal - IOS SDK - 企业账户/个体户

ios - 使用未解析的标识符 FBSDKAppEventNamePurchased

iphone - 关于单位换算的简单问题

ios - 如何知道 Root View Controller View 层次结构已完成加载