@@ -21,6 +21,8 @@ @interface QNProgessDelegate : NSObject
2121- (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context ;
2222@property (nonatomic , strong ) QNInternalProgressBlock progressBlock;
2323@property (nonatomic , strong ) NSProgress *progress;
24+ @property (nonatomic , strong ) NSURLSessionUploadTask *task;
25+ @property (nonatomic , strong ) QNCancelBlock cancelBlock;
2426- (instancetype )initWithProgress : (QNInternalProgressBlock)progressBlock ;
2527@end
2628
@@ -51,6 +53,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
5153 void *p = (__bridge void *)(self);
5254 if (p == context) {
5355 _progressBlock (progress.completedUnitCount , progress.totalUnitCount );
56+ if (_cancelBlock && _cancelBlock ()) {
57+ [_task cancel ];
58+ }
5459 }
5560 else {
5661 [super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
@@ -126,7 +131,8 @@ + (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
126131
127132- (void ) sendRequest : (NSMutableURLRequest *)request
128133 withCompleteBlock : (QNCompleteBlock)completeBlock
129- withProgressBlock : (QNInternalProgressBlock)progressBlock {
134+ withProgressBlock : (QNInternalProgressBlock)progressBlock
135+ withCancelBlock : (QNCancelBlock)cancelBlock {
130136 __block NSDate *startTime = [NSDate date ];
131137 NSProgress *progress = nil ;
132138 __block NSString *host = request.URL .host ;
@@ -188,6 +194,8 @@ - (void) sendRequest:(NSMutableURLRequest *)request
188194 if (progress != nil ) {
189195 [progress addObserver: delegate forKeyPath: @" fractionCompleted" options: NSKeyValueObservingOptionNew context: (__bridge void *)delegate];
190196 delegate.progress = progress;
197+ delegate.task = uploadTask;
198+ delegate.cancelBlock = cancelBlock;
191199 }
192200
193201 [request setTimeoutInterval: _timeout];
@@ -216,7 +224,8 @@ - (void)multipartPost:(NSString *)url
216224 error: nil ];
217225 [self sendRequest: request
218226 withCompleteBlock: completeBlock
219- withProgressBlock: progressBlock];
227+ withProgressBlock: progressBlock
228+ withCancelBlock: cancelBlock];
220229}
221230
222231- (void ) post : (NSString *)url
@@ -240,7 +249,8 @@ - (void) post:(NSString *)url
240249 QNAsyncRun ( ^{
241250 [self sendRequest: request
242251 withCompleteBlock: completeBlock
243- withProgressBlock: progressBlock];
252+ withProgressBlock: progressBlock
253+ withCancelBlock: cancelBlock];
244254 });
245255}
246256
0 commit comments