2013-12-16 73 views
0

我使用AFNetworking 2.0創建自己的協議和委託。使用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

你可以用'委託method'或通知中心。 –

+0

我該怎麼做?因爲當下載完成時,正確調用方法「(void)didDownloadMap」。但我不知道如何繼續在我的應用程序的另一個類中使用它:/ – Lapinou

回答

3

請參考NSNOtification Class。要知道下載是否完成

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

並調用類的ViewDidLoad方法 請撥打本該是(void)didDownloadMap方法

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

非常好!它完美的工作!非常感謝!現在,我明白它是如何工作的:) – Lapinou