我似乎遇到了麻烦。出于某种原因,如果我尝试检查NSURLSessionTask
对象的类类型,它根本不起作用。如果我检查它们的taskDescription
属性,如果我在启动任务之前设置它们,这当然有效。我只想知道为什么下面的代码对我不起作用。感谢您提供的任何帮助!
- (void)uploadIt
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
defaultConfigObject.timeoutIntervalForResource = 15.0;
defaultConfigObject.requestCachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURLSessionUploadTask *uploadTask = [defaultSession uploadTaskWithRequest:someRequest fromData:body];
[uploadTask resume];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (self.uploadResponseData)
{
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:self.uploadResponseData options:kNilOptions error:nil];
NSLog(@"Dict: %@",d);
}
if ([task isKindOfClass:[NSURLSessionDownloadTask class]])
{
// Not called
}
else if ([task isKindOfClass:[NSURLSessionUploadTask class]])
{
// Not called
}
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
if (!self.uploadResponseData)
{
self.uploadResponseData = [NSMutableData dataWithData:data];
}
else
{
[self.uploadResponseData appendData:data];
}
}