自定义委托不是从NSURLSessionDelegates调用

时间:2016-11-15 15:04:28

标签: ios objective-c delegates nsurlsession

我正在使用NSURLSession进行服务集成。在将图像上传到服务器时,我想显示已上载到服务器的数据百分比。为此,我在服务模型类中创建了一个自定义委托,并在NSURLSession的委托中调用此委托。请检查下面的代码。

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
NSLog(@" %lld    %lld    %lld %lu", bytesSent , totalBytesSent,totalBytesExpectedToSend,(unsigned long)task.taskIdentifier);

float dataSent = (totalBytesSent/totalBytesExpectedToSend);

NSLog(@"sent percentage : %0.2f",(dataSent * 100));
dispatch_async(dispatch_get_main_queue(), ^{

//_delegate is my custom delegate
    [_delegate updateUploadedDataInfo:dataSent];
});
}

但是,[_delegate updateUploadedDataInfo:dataSent]方法不在这里调用。 请建议我在哪里做错了。

1 个答案:

答案 0 :(得分:1)

只需将服务模型对象添加为委托即可。例如,假设您的servcice模型类是MyModel,并且您发布的代码位于另一个类MyDownloader中。在MyModel类中添加:

MyDonlowder *downlowder = [[MyDownloader alloc] init];
downlowder.delegate = self;

还要使MyModel类符合MyCystomDelegateProtocol并实现所需的方法updateUploadedDataInfo