【问题标题】:Create own delegate and protocols using AFNetworking 2.0使用 AFNetworking 2.0 创建自己的委托和协议
【发布时间】: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


【解决方案1】:

请参考NSNOtification Class。 请在你想知道下载是否完成的类的ViewDidLoad方法中调用这个

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

并称之为(void)didDownloadMap方法

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

【讨论】:

  • 非常好!它完美地工作!非常感谢!现在,我明白了它是如何工作的 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多