Today's Menu  Portugal
journal and plan nutrition
Z5URLSessionController.m
Go to the documentation of this file.
1 //
2 // Z5URLSessionController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 6/8/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
10 
11 @interface Z5URLSessionController ()
12 @property (nonatomic,strong) NSURLSession *session;
13 @property (nonatomic,strong) NSURLSessionDataTask *finderTask;
14 @end
15 
16 @implementation Z5URLSessionController
17 
18 
19 #pragma mark Find Using NSURLSession Delegate Approach
20 - (NSURLSession *)createSession
21 {
22  static NSURLSession *session = nil;
23  session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
24  delegate:self
25  delegateQueue:[NSOperationQueue mainQueue]];
26  return session;
27 }
28 
29 
31 {
32  self.session = [self createSession];
33 
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];
38 }
39 
40 
41 - (void)URLSession:(NSURLSession *)session
42  dataTask:(NSURLSessionDataTask *)dataTask
43  didReceiveData:(NSData *)data{
44 
45  NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
46  NSLog(@"URLSessController.didReceiveData: raw: %@", newStr);
47 
48  // NSError *err = nil;
49  // // NSError *err = [[NSError errorWithDomain:(nonnull NSErrorDomain) code:<#(NSInteger)#> userInfo:<#(nullable NSDictionary<NSErrorUserInfoKey,id> *)#>];
50  // NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
51  // NSLog(@"URLSession.didReceiveData: err: %@", err);
52  // if (nil == err) {
53  // [self.responseDataTextView setString:[dataArray description]];
54  // [self hideProgressIndicator];
55  // } else {
56  // NSLog(@"URLSession.didReceiveData: err: %@", err);
57  // }
58 }
59 
60 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
61  willCacheResponse:(NSCachedURLResponse *)proposedResponse
62  completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler{
63 
64  NSLog(@"Here now: %s",__func__);
65 }
66 
67 - (void)URLSession:(NSURLSession *)session
68  task:(NSURLSessionTask *)task
69 didCompleteWithError:(NSError *)error{
70  NSLog(@"Here now: %s",__func__);
71 }
72 
73 // -(void) fetchEmployeeDataWithCompletionBlock:(void(^)(NSArray *employeeArray, NSError *error)) completionBlock
74 // {
75 // NSLog(@"main thread? ANS - %@",[NSThread isMainThread]? @"YES":@"NO");
76 // NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
77 //
78 // NSString *cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"/nsurlsessiondemo.cache"];
79 //
80 // NSURLCache *myCache = [[NSURLCache alloc] initWithMemoryCapacity: 16384
81 // diskCapacity: 268435456
82 // diskPath: cachePath];
83 // defaultConfigObject.URLCache = myCache;
84 //
85 // defaultConfigObject.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
86 //
87 // NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration: defaultConfigObject
88 // delegate: nil
89 // delegateQueue: [NSOperationQueue mainQueue]];
90 //
91 //
92 // // NSURL *dataURL = [NSURL URLWithString:EMPLOYEE_DATA_URL];
93 // //NSURL *dataURL = [NSURL URLWithString:@"http://google.com"];
94 // // NSURL *dataURL = [NSURL URLWithString:@"https://api.nal.usda.gov/ndb/list?format=json&lt=f&sort=n&api_key=MHeHC2wqKJHmMxYqHzzLlMfnl4ZOLQLAZeKGEkdK"];
95 // NSURL *dataURL = [NSURL URLWithString:@"https://api.nal.usda.gov/ndb/list?format=json&lt=f&sort=n&offset=51&api_key=MHeHC2wqKJHmMxYqHzzLlMfnl4ZOLQLAZeKGEkdK"];
96 //
97 // NSURLRequest *request = [NSURLRequest requestWithURL:dataURL];
98 //
99 // [[delegateFreeSession dataTaskWithRequest:request
100 // completionHandler:^(NSData *data, NSURLResponse *response,
101 // NSError *error)
102 // {
103 // NSLog(@"z5URLSAessionControl.fetchEmployeeData: Got response %@ with error %@.\n", response, error);
104 // NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
105 // completionBlock (dataArray, error);
106 // }
107 // ]resume];
108 // }
109 -(void)fetchUSDAdata:(NSURLRequest *)request withCompletionBlock:(void(^)(NSDictionary *informationDictionary, NSError *error)) completionBlock
110 {
111  NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
112 
113  NSString *cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"/nsurlsessiondemo.cache"];
114 
115  NSURLCache *myCache = [[NSURLCache alloc] initWithMemoryCapacity: 16384
116  diskCapacity: 268435456
117  diskPath: cachePath];
118  defaultConfigObject.URLCache = myCache;
119 
120  defaultConfigObject.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
121 
122  NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration: defaultConfigObject
123  delegate: nil
124  delegateQueue: [NSOperationQueue mainQueue]];
125  // NSURL *dataURL = [NSURL URLWithString:@"https://api.nal.usda.gov/ndb/list?format=json&lt=f&sort=n&offset=51&api_key=MHeHC2wqKJHmMxYqHzzLlMfnl4ZOLQLAZeKGEkdK"];
126  //
127  // NSURLRequest *request = [NSURLRequest requestWithURL:dataURL];
128 
129  [[delegateFreeSession dataTaskWithRequest:request
130  completionHandler:^(NSData *data, NSURLResponse *response,
131  NSError *error)
132  {
133  NSLog(@"Z5URLSessionControl.fetchUSDAdata: Got response %@ with error %@.\n", response, error);
134  if (data) {
135  NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
136  completionBlock (dataDictionary, error);
137  } else {
138  NSLog(@"Z5URLSessionControl.fetchUSDAdata: ERROR: %@", error);
139  }
140  }
141  ] resume];
142 
143 }
144 @end