12 @property (nonatomic,strong) NSURLSession *session;
13 @property (nonatomic,strong) NSURLSessionDataTask *finderTask;
19 #pragma mark Find Using NSURLSession Delegate Approach 22 static NSURLSession *
session = nil;
23 session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
25 delegateQueue:[NSOperationQueue mainQueue]];
34 NSURL *dataURL = [NSURL URLWithString:@"http://google.com"];
35 NSURLRequest *request = [NSURLRequest requestWithURL:dataURL];
36 self.finderTask = [
self.session dataTaskWithRequest:request];
37 [
self.finderTask resume];
41 - (void)URLSession:(NSURLSession *)session
42 dataTask:(NSURLSessionDataTask *)dataTask
43 didReceiveData:(NSData *)data{
45 NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
46 NSLog(
@"URLSessController.didReceiveData: raw: %@", newStr);
60 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
61 willCacheResponse:(NSCachedURLResponse *)proposedResponse
62 completionHandler:(
void (^)(NSCachedURLResponse *cachedResponse))completionHandler{
64 NSLog(
@"Here now: %s",__func__);
67 - (void)URLSession:(NSURLSession *)session
68 task:(NSURLSessionTask *)task
69 didCompleteWithError:(NSError *)error{
70 NSLog(
@"Here now: %s",__func__);
109 -(void)fetchUSDAdata:(NSURLRequest *)request withCompletionBlock:(
void(^)(NSDictionary *informationDictionary, NSError *error)) completionBlock
111 NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
113 NSString *cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"/nsurlsessiondemo.cache"];
115 NSURLCache *myCache = [[NSURLCache alloc] initWithMemoryCapacity: 16384
116 diskCapacity: 268435456
117 diskPath: cachePath];
118 defaultConfigObject.URLCache = myCache;
120 defaultConfigObject.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
122 NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration: defaultConfigObject
124 delegateQueue: [NSOperationQueue mainQueue]];
129 [[delegateFreeSession dataTaskWithRequest:request
130 completionHandler:^(NSData *data, NSURLResponse *response,
133 NSLog(@"Z5URLSessionControl.fetchUSDAdata: Got response %@ with error %@.\n", response, error);
135 NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
136 completionBlock (dataDictionary, error);
138 NSLog(@"Z5URLSessionControl.fetchUSDAdata: ERROR: %@", error);
NSURLSession * createSession()
void findDataUsingDelegate()