Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CDVWebViewUIDelegate.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import "CDVWebViewUIDelegate.h"
  18. @implementation CDVWebViewUIDelegate
  19. - (instancetype)initWithTitle:(NSString*)title
  20. {
  21. self = [super init];
  22. if (self) {
  23. self.title = title;
  24. windows = [[NSMutableArray alloc] init];
  25. }
  26. return self;
  27. }
  28. - (void) webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message
  29. initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(void))completionHandler
  30. {
  31. UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
  32. message:message
  33. preferredStyle:UIAlertControllerStyleAlert];
  34. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
  35. style:UIAlertActionStyleDefault
  36. handler:^(UIAlertAction* action)
  37. {
  38. completionHandler();
  39. [alert dismissViewControllerAnimated:YES completion:nil];
  40. }];
  41. [alert addAction:ok];
  42. UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
  43. [rootController presentViewController:alert animated:YES completion:nil];
  44. }
  45. - (void) webView:(WKWebView*)webView runJavaScriptConfirmPanelWithMessage:(NSString*)message
  46. initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(BOOL result))completionHandler
  47. {
  48. UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
  49. message:message
  50. preferredStyle:UIAlertControllerStyleAlert];
  51. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
  52. style:UIAlertActionStyleDefault
  53. handler:^(UIAlertAction* action)
  54. {
  55. completionHandler(YES);
  56. [alert dismissViewControllerAnimated:YES completion:nil];
  57. }];
  58. [alert addAction:ok];
  59. UIAlertAction* cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel")
  60. style:UIAlertActionStyleDefault
  61. handler:^(UIAlertAction* action)
  62. {
  63. completionHandler(NO);
  64. [alert dismissViewControllerAnimated:YES completion:nil];
  65. }];
  66. [alert addAction:cancel];
  67. UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
  68. [rootController presentViewController:alert animated:YES completion:nil];
  69. }
  70. - (void) webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt
  71. defaultText:(NSString*)defaultText initiatedByFrame:(WKFrameInfo*)frame
  72. completionHandler:(void (^)(NSString* result))completionHandler
  73. {
  74. UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
  75. message:prompt
  76. preferredStyle:UIAlertControllerStyleAlert];
  77. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
  78. style:UIAlertActionStyleDefault
  79. handler:^(UIAlertAction* action)
  80. {
  81. completionHandler(((UITextField*)alert.textFields[0]).text);
  82. [alert dismissViewControllerAnimated:YES completion:nil];
  83. }];
  84. [alert addAction:ok];
  85. UIAlertAction* cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel")
  86. style:UIAlertActionStyleDefault
  87. handler:^(UIAlertAction* action)
  88. {
  89. completionHandler(nil);
  90. [alert dismissViewControllerAnimated:YES completion:nil];
  91. }];
  92. [alert addAction:cancel];
  93. [alert addTextFieldWithConfigurationHandler:^(UITextField* textField) {
  94. textField.text = defaultText;
  95. }];
  96. UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
  97. [rootController presentViewController:alert animated:YES completion:nil];
  98. }
  99. - (WKWebView*) webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration forNavigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures
  100. {
  101. if (!navigationAction.targetFrame.isMainFrame) {
  102. if (self.allowNewWindows) {
  103. WKWebView* v = [[WKWebView alloc] initWithFrame:webView.frame configuration:configuration];
  104. v.UIDelegate = webView.UIDelegate;
  105. v.navigationDelegate = webView.navigationDelegate;
  106. UIViewController* vc = [[UIViewController alloc] init];
  107. vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  108. vc.view = v;
  109. [windows addObject:vc];
  110. UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
  111. [rootController presentViewController:vc animated:YES completion:nil];
  112. return v;
  113. } else {
  114. [webView loadRequest:navigationAction.request];
  115. }
  116. }
  117. return nil;
  118. }
  119. - (void)webViewDidClose:(WKWebView*)webView
  120. {
  121. for (UIViewController* vc in windows) {
  122. if (vc.view == webView) {
  123. [vc dismissViewControllerAnimated:YES completion:nil];
  124. [windows removeObject:vc];
  125. break;
  126. }
  127. }
  128. // We do not allow closing the primary WebView
  129. }
  130. @end