Today's Menu  Portugal
journal and plan nutrition
U4WebViewController.m
Go to the documentation of this file.
1 //
2 // U4WebViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 5/29/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
9 #import "U4WebViewController.h"
10 #import "Y3WebView.h"
13 
14 @interface U4WebViewController ()
15 
16 @end
93 @implementation U4WebViewController {
94  Y3WebView *webPanel;
95  UIPasteboard *pasteboard;
96  NSURL *fileURL;
98  NSString *mostRecentTitle;
101 }
102 
103 - (void)viewDidLoad {
104  [super viewDidLoad];
105 
106  CGRect totalFrame = self.view.frame;
107  CGRect topNaviFrame = [[[self navigationController] navigationBar] frame];
108  float availableWindowTop = topNaviFrame.origin.y + topNaviFrame.size.height;
109 
110 
111  CGRect webViewFrame = CGRectMake(8,
112  availableWindowTop + 4,
113  totalFrame.size.width - 16,
114  totalFrame.size.height - availableWindowTop - 8);
115 
116 // WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
117 // config.showConsole = YES;
118 
119  webPanel = [[Y3WebView alloc] initWithFrame:webViewFrame andDelegate:self];
120 
121 
122  [self.view addSubview:webPanel];
123 
124  NSURL *nsurl=[NSURL URLWithString:@"http://google.com"];
125  NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
126  [webPanel.webView loadRequest:nsrequest];
127 
128  mostRecentJavascriptReturnStr = [[NSString alloc] init];
129 }
130 
131 -(void) viewWillAppear:(BOOL)animated
132 {
133  [webPanel.webView setBackgroundColor:[UIColor grayColor]];
134  [webPanel setBackgroundColor:[UIColor colorWithHue:0.22 saturation:1.0 brightness:0.6 alpha:1.0]];
135 }
136 - (void) userContentController:(WKUserContentController*)userContentController
137  didReceiveScriptMessage:(WKScriptMessage*)message
138 {
139  // A new selected text has been received
140  NSLog(@"U4WebViewControler.userContentController-didReceiveScriptMessage");
141  if ([message.body isKindOfClass:[NSString class]])
142  {
143  NSLog(@"U4WebViewControler.userContentController: string");
144  }
145 }
146 
147 - (void) becomeResponder:(BOOL) flag
148 {
149  [webPanel becomeResponder:flag];
150  [self.view setNeedsLayout];
151  [self.view setNeedsDisplay];
152 
153  [self.view snapshotViewAfterScreenUpdates:YES];
154 }
155 
156 - (BOOL) jsReturnIndication:(NSString *)string
157 {
158  // NSLog(@"U4WebViewController.javaReturn: %@", string);
159  BOOL ret;
160  if (string) {
161  mostRecentJavascriptReturnStr = [string copy];
162  [webPanel.webControls setBackgroundColor:[UIColor greenColor]];
163  ret = YES;
164  } else {
165  mostRecentJavascriptReturnStr = @"Null return";
166  [webPanel.webControls setBackgroundColor:[UIColor orangeColor]];
167  ret = NO;
168  }
169  return ret;
170 }
171 - (void) jsTitleReturn:(NSString *)string
172 {
173  NSLog(@"U4WebViewController.jsTitleReturn: string: %@", string);
174  if ([self jsReturnIndication:string]) {
175  mostRecentTitle = [string copy];
176  }
177 }
178 - (void) jsIngredientsReturn:(NSString *)string
179 {
180  if ([self jsReturnIndication:string]) {
181  mostRecentIngredients = [string copy];
182  }
183 }
184 - (void) jsInstructionReturn:(NSString *)string
185 {
186  if ([self jsReturnIndication:string]) {
187  mostRecentInstruction = [string copy];
188  }
189 }
190 
191 - (void)logCopy
192 {
193  NSString *displayString = @"WEB COPY";
194  NSArray<NSString *> *pasteStrings = [[UIPasteboard generalPasteboard] pasteboardTypes];
195  // NSLog(@"U4WebViewController.logCopy: pasteStrings: %@", pasteStrings);
196  for (NSString *pasteStr in pasteStrings) {
197  // NSLog(@"U4WebViewController.logCopy: pasteStr: %@", pasteStr);
198  NSObject *valPasteType = [pasteboard valueForPasteboardType:pasteStr];
199  if (nil != valPasteType) {
200  NSLog(@"U4WebViewController.logCopy: has paste type: %@", pasteStr);
201 
202  NSString *addPasteType = [NSString stringWithFormat: @"%@ - %@", @"\n", pasteStr];
203  displayString = [displayString stringByAppendingString:addPasteType];
204 
205  if ([@"public.utf8-plain-text" isEqualToString:pasteStr]) {
206  NSObject *obj5 = [pasteboard valueForPasteboardType:pasteStr];
207  NSString *addInfo = [NSString stringWithFormat: @"%@", obj5];
208  displayString = [displayString stringByAppendingString:addInfo];
209  }
210  }
211  }
212  NSLog(@"U4WebViewController.logCopy: displayString: %@", displayString);
213 }
214 - (void) setControlbarColor:(UIColor *)color
215 {
216  [webPanel.webControls setBackgroundColor:color];
217 }
218 
219 - (void) fireSnippetScreen
220 {
221  NSLog(@"U4WebViewController.fireSnippetScreen: w/title: %@", mostRecentTitle);
222  W7WebSnippetViewController *parameterView =
223  [[W7WebSnippetViewController alloc] initWithNibName:nil bundle:nil];
224 
225  [parameterView setShowHtmlTitle:mostRecentTitle];
226 
227  NSAttributedString *titleSimple = [[NSAttributedString alloc] initWithData:[mostRecentTitle dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];
228  [parameterView setShowTextTitle:[titleSimple string]];
229 
230 
231  [parameterView setShowHtmlIngredients:mostRecentIngredients];
232  [parameterView setShowHtmlInstruction:mostRecentInstruction];
233 
234  [self.navigationController pushViewController:parameterView animated:YES];
235 }
236 #pragma mark - WKUIDelegate
237 
238 - (void)webView:(WKWebView *)webView2 didFinishNavigation:(WKNavigation *)navigation
239 {
240  [webPanel updateWebControls];
241  [webPanel.webControls setBackgroundColor:[UIColor blueColor]];
242  [webPanel scanPageForTitle];
243  [webPanel scanPageForIngredients];
244  [webPanel scanPageForInstruction];
245  NSLog(@"WebViewController.didFinishNavigation");
246 }
247 
248 - (void)webView:(WKWebView *)webView
249 runJavaScriptAlertPanelWithMessage:(NSString *)message
250 initiatedByFrame:(WKFrameInfo *)frame
251 completionHandler:(void (^)(void))completionHandler
252 {
253  NSLog(@"U4WebViewController:runJavaScriptAlertPanelWithMessage");
254 }
255 
256 - (void)webView:(WKWebView *)webView
257 runJavaScriptConfirmPanelWithMessage:(NSString *)message
258 initiatedByFrame:(WKFrameInfo *)frame
259 completionHandler:(void (^)(BOOL result))completionHandler
260 {
261  NSLog(@"U4WebViewController:runJavaScriptConfirmPanelWithMessage");
262 }
263 
264 
265 - (void)webView:(WKWebView *)webView
266 runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt
267  defaultText:(NSString *)defaultText
268 initiatedByFrame:(WKFrameInfo *)frame
269 completionHandler:(void (^)(NSString *result))completionHandler
270 {
271  NSLog(@"U4WebViewController:runJavaScriptTextInputPanelWithPrompt");
272 }
273 
274 
275 #pragma mark - WKNavigationDelegate
276 
277 - (void)webView:(WKWebView *)webView
278 didCommitNavigation:(WKNavigation *)navigation
279 {
280  // NSLog(@"U4WebViewController.didCommitNavigation: active URL: %@", webView.URL.description);
281 }
282 
283 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
284 {
285  NSLog(@"U4WebViewController.didStartProvisionalNavigation: active URL: %@", webView.URL.description);
286  [webPanel.webControls setBackgroundColor:[UIColor whiteColor]];
287 }
288 
289 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
290  NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
291  NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
292  for (NSHTTPCookie *cookie in cookies) {
293  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
294  }
295 
296  decisionHandler(WKNavigationResponsePolicyAllow);
297  //NSLog(@"decidePolicyForNavigationResponse");
298 }
299 
300 
301 //Download manager
302 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
303 
304  NSURLRequest *request = navigationAction.request;
305  fileURL = request.URL;
306 
307 // HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
308 // UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:dlvc];
309 // vc.transitioningDelegate = self;
310 // dlvc.delegate = self;
311 
312  // NSLog(@"U4WebViewController.decidePolicyForNavigationAction: ~~~~~~~~~~~~~~~~~~~~~~");
313  // [self logNavigationAction:navigationAction];
314  if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
315 
316  //Internal file links
317  // NSString *internalFileExtension = fileURL.absoluteString.pathExtension;
318 // if ([fileExtensions containsObject:[internalFileExtension lowercaseString]]) {
319 //
320 // //Fire download
321 // // TODO: DLVC [dlvc downloadURL:fileURL userInfo:nil];
322 //
323 //
324 // [self presentViewController:vc animated:YES completion:nil];
325 // [vc release];
326 //
327 // NSLog(@"internalURL is %@", fileURL);
328 // if (decisionHandler) {
329 // decisionHandler(WKNavigationActionPolicyCancel);
330 // }
331 // return;
332 // }
333 //
334 // //External file extensions
335 // NSString *externalFileExtension = fileURL.pathExtension;
336 // if ([fileExtensions containsObject:[externalFileExtension lowercaseString]]) {
337 //
338 // //Fire download
339 // [dlvc downloadURL:fileURL userInfo:nil];
340 // [self presentViewController:vc animated:YES completion:nil];
341 // [vc release];
342 //
343 // NSLog(@"externalURL is %@", fileURL);
344 // if (decisionHandler) {
345 // decisionHandler(WKNavigationActionPolicyCancel);
346 // }
347 // return;
348 // }
349  }
350 
351  if (decisionHandler) {
352  decisionHandler(WKNavigationActionPolicyAllow);
353  }
354 }
355 
356 #pragma mark - controls
357 
358 //- (void) clickBackButton
359 //{
360 // [webPanel.webView goBack];
361 //}
362 //
363 //- (void) clickChecklistButton
364 //{
365 // NSLog(@"U4WebViewController.clickChecklistButton");
366 // NSBundle *mb = [NSBundle mainBundle];
367 // NSURL *nsurl=[mb URLForResource:@"testHalibut" withExtension:@"html"];
368 // NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
369 // [webPanel.webView loadRequest:nsrequest];
370 //}
371 //- (void) clickStirButton
372 //{
373 // NSLog(@"U4WebViewController.clickStirButton");
374 // [webPanel stirJavascript];
375 //}
376 //- (void) clickForwardButton
377 //{
378 // [webPanel.webView goForward];
379 //}
380 
381 //- (void) logNavigationAction: (WKNavigationAction *)navigationAction
382 //{
383 // NSLog(@" request: %@", navigationAction.request);
384 // NSLog(@" source frame: %@", navigationAction.sourceFrame);
385 // NSLog(@" target frame: %@", navigationAction.targetFrame);
386 // if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
387 // NSLog(@" WKNavigationTypeLinkActivated");
388 // }
389 // if (navigationAction.navigationType == WKNavigationTypeFormSubmitted) {
390 // NSLog(@" WKNavigationTypeFormSubmitted");
391 // }
392 // if (navigationAction.navigationType == WKNavigationTypeBackForward) {
393 // NSLog(@" WKNavigationTypeBackForward");
394 // }
395 // if (navigationAction.navigationType == WKNavigationTypeReload) {
396 // NSLog(@" WKNavigationTypeReload");
397 // }
398 // if (navigationAction.navigationType == WKNavigationTypeFormResubmitted) {
399 // NSLog(@" WKNavigationTypeFormResubmitted");
400 // }
401 // if (navigationAction.navigationType == WKNavigationTypeOther) {
402 // NSLog(@" WKNavigationTypeOther");
403 // }
404 //}
405 /*
406 #pragma mark - Navigation
407 
408 // In a storyboard-based application, you will often want to do a little preparation before navigation
409 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
410  // Get the new view controller using [segue destinationViewController].
411  // Pass the selected object to the new view controller.
412 }
413 */
414 - (void)didReceiveMemoryWarning {
415  [super didReceiveMemoryWarning];
416  // Dispose of any resources that can be recreated.
417  NSLog(@"U4WebViewController.didReceiveMemoryWarning");
418 }
419 
420 @end
421 
NSString * mostRecentJavascriptReturnStr
UIPasteboard * pasteboard
NSString * mostRecentTitle
WKWebView * webView
NSString * mostRecentIngredients
NSURL * fileURL
NSString * mostRecentInstruction