CDVWKWebViewUIDelegate.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "CDVWKWebViewUIDelegate.h"
  18. @implementation CDVWKWebViewUIDelegate
  19. - (instancetype)initWithTitle:(NSString*)title
  20. {
  21. self = [super init];
  22. if (self) {
  23. self.title = title;
  24. }
  25. return self;
  26. }
  27. - (void) webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message
  28. initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(void))completionHandler
  29. {
  30. UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
  31. message:message
  32. preferredStyle:UIAlertControllerStyleAlert];
  33. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
  34. style:UIAlertActionStyleDefault
  35. handler:^(UIAlertAction* action)
  36. {
  37. completionHandler();
  38. [alert dismissViewControllerAnimated:YES completion:nil];
  39. }];
  40. [alert addAction:ok];
  41. UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
  42. [rootController presentViewController:alert animated:YES completion:nil];
  43. }
  44. - (void) webView:(WKWebView*)webView runJavaScriptConfirmPanelWithMessage:(NSString*)message
  45. initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(BOOL result))completionHandler
  46. {
  47. UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
  48. message:message
  49. preferredStyle:UIAlertControllerStyleAlert];
  50. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
  51. style:UIAlertActionStyleDefault
  52. handler:^(UIAlertAction* action)
  53. {
  54. completionHandler(YES);
  55. [alert dismissViewControllerAnimated:YES completion:nil];
  56. }];
  57. [alert addAction:ok];
  58. UIAlertAction* cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel")
  59. style:UIAlertActionStyleDefault
  60. handler:^(UIAlertAction* action)
  61. {
  62. completionHandler(NO);
  63. [alert dismissViewControllerAnimated:YES completion:nil];
  64. }];
  65. [alert addAction:cancel];
  66. UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
  67. [rootController presentViewController:alert animated:YES completion:nil];
  68. }
  69. - (void) webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt
  70. defaultText:(NSString*)defaultText initiatedByFrame:(WKFrameInfo*)frame
  71. completionHandler:(void (^)(NSString* result))completionHandler
  72. {
  73. UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
  74. message:prompt
  75. preferredStyle:UIAlertControllerStyleAlert];
  76. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
  77. style:UIAlertActionStyleDefault
  78. handler:^(UIAlertAction* action)
  79. {
  80. completionHandler(((UITextField*)alert.textFields[0]).text);
  81. [alert dismissViewControllerAnimated:YES completion:nil];
  82. }];
  83. [alert addAction:ok];
  84. UIAlertAction* cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel")
  85. style:UIAlertActionStyleDefault
  86. handler:^(UIAlertAction* action)
  87. {
  88. completionHandler(nil);
  89. [alert dismissViewControllerAnimated:YES completion:nil];
  90. }];
  91. [alert addAction:cancel];
  92. [alert addTextFieldWithConfigurationHandler:^(UITextField* textField) {
  93. textField.text = defaultText;
  94. }];
  95. UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
  96. [rootController presentViewController:alert animated:YES completion:nil];
  97. }
  98. @end