Today's Menu  Portugal
journal and plan nutrition
Instance Methods | Properties | List of all members
Z5URLSessionController Class Reference

#import <Z5URLSessionController.h>

Inheritance diagram for Z5URLSessionController:
Inheritance graph
[legend]
Collaboration diagram for Z5URLSessionController:
Collaboration graph
[legend]

Instance Methods

(void) - findDataUsingDelegate
 
(void) - fetchUSDAdata:withCompletionBlock:
 
(NSURLSession *) - createSession [implementation]
 
(void) - URLSession:dataTask:didReceiveData: [implementation]
 
(void) - URLSession:dataTask:willCacheResponse:completionHandler: [implementation]
 
(void) - URLSession:task:didCompleteWithError: [implementation]
 

Properties

NSURLSession * session [implementation]
 
NSURLSessionDataTask * finderTask [implementation]
 

Detailed Description

Definition at line 11 of file Z5URLSessionController.h.

Method Documentation

◆ createSession()

- (NSURLSession *) createSession
implementation

Definition at line 20 of file Z5URLSessionController.m.

21 {
22  static NSURLSession *session = nil;
23  session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
24  delegate:self
25  delegateQueue:[NSOperationQueue mainQueue]];
26  return session;
27 }

◆ fetchUSDAdata:withCompletionBlock:()

- (void) fetchUSDAdata: (NSURLRequest *)  request
withCompletionBlock: (void(^)(NSDictionary *informationDictionary, NSError *error))  incomingDictionary 

Definition at line 109 of file Z5URLSessionController.m.

109  :(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 }

◆ findDataUsingDelegate()

- (void) findDataUsingDelegate

Definition at line 30 of file Z5URLSessionController.m.

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 }

◆ URLSession:dataTask:didReceiveData:()

- (void) URLSession: (NSURLSession *)  session
dataTask: (NSURLSessionDataTask *)  dataTask
didReceiveData: (NSData *)  data 
implementation

Definition at line 41 of file Z5URLSessionController.m.

41  :(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 }

◆ URLSession:dataTask:willCacheResponse:completionHandler:()

- (void) URLSession: (NSURLSession *)  session
dataTask: (NSURLSessionDataTask *)  dataTask
willCacheResponse: (NSCachedURLResponse *)  proposedResponse
completionHandler: (void(^)(NSCachedURLResponse *cachedResponse))  completionHandler 
implementation

Definition at line 60 of file Z5URLSessionController.m.

60  :(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
61  willCacheResponse:(NSCachedURLResponse *)proposedResponse
62  completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler{
63 
64  NSLog(@"Here now: %s",__func__);
65 }

◆ URLSession:task:didCompleteWithError:()

- (void) URLSession: (NSURLSession *)  session
task: (NSURLSessionTask *)  task
didCompleteWithError: (NSError *)  error 
implementation

Definition at line 67 of file Z5URLSessionController.m.

67  :(NSURLSession *)session
68  task:(NSURLSessionTask *)task
69 didCompleteWithError:(NSError *)error{
70  NSLog(@"Here now: %s",__func__);
71 }

Property Documentation

◆ finderTask

- (NSURLSessionDataTask*) finderTask
readwritenonatomicstrongimplementation

Definition at line 13 of file Z5URLSessionController.m.

◆ session

- (NSURLSession*) session
readwritenonatomicstrongimplementation

Definition at line 12 of file Z5URLSessionController.m.


The documentation for this class was generated from the following files: