123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- */
-
- #import "CDVWebViewUIDelegate.h"
-
- @implementation CDVWebViewUIDelegate
-
- - (instancetype)initWithTitle:(NSString*)title
- {
- self = [super init];
- if (self) {
- self.title = title;
- windows = [[NSMutableArray alloc] init];
- }
-
- return self;
- }
-
- - (void) webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message
- initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(void))completionHandler
- {
- UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
- message:message
- preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction* action)
- {
- completionHandler();
- [alert dismissViewControllerAnimated:YES completion:nil];
- }];
-
- [alert addAction:ok];
-
- UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
-
- [rootController presentViewController:alert animated:YES completion:nil];
- }
-
- - (void) webView:(WKWebView*)webView runJavaScriptConfirmPanelWithMessage:(NSString*)message
- initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(BOOL result))completionHandler
- {
- UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
- message:message
- preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction* action)
- {
- completionHandler(YES);
- [alert dismissViewControllerAnimated:YES completion:nil];
- }];
-
- [alert addAction:ok];
-
- UIAlertAction* cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel")
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction* action)
- {
- completionHandler(NO);
- [alert dismissViewControllerAnimated:YES completion:nil];
- }];
- [alert addAction:cancel];
-
- UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
-
- [rootController presentViewController:alert animated:YES completion:nil];
- }
-
- - (void) webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt
- defaultText:(NSString*)defaultText initiatedByFrame:(WKFrameInfo*)frame
- completionHandler:(void (^)(NSString* result))completionHandler
- {
- UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title
- message:prompt
- preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction* action)
- {
- completionHandler(((UITextField*)alert.textFields[0]).text);
- [alert dismissViewControllerAnimated:YES completion:nil];
- }];
-
- [alert addAction:ok];
-
- UIAlertAction* cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel")
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction* action)
- {
- completionHandler(nil);
- [alert dismissViewControllerAnimated:YES completion:nil];
- }];
- [alert addAction:cancel];
-
- [alert addTextFieldWithConfigurationHandler:^(UITextField* textField) {
- textField.text = defaultText;
- }];
-
- UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
-
- [rootController presentViewController:alert animated:YES completion:nil];
- }
-
- - (WKWebView*) webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration forNavigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures
- {
- if (!navigationAction.targetFrame.isMainFrame) {
- if (self.allowNewWindows) {
- WKWebView* v = [[WKWebView alloc] initWithFrame:webView.frame configuration:configuration];
- v.UIDelegate = webView.UIDelegate;
- v.navigationDelegate = webView.navigationDelegate;
-
- UIViewController* vc = [[UIViewController alloc] init];
- vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
- vc.view = v;
-
- [windows addObject:vc];
-
- UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
- [rootController presentViewController:vc animated:YES completion:nil];
- return v;
- } else {
- [webView loadRequest:navigationAction.request];
- }
- }
-
- return nil;
- }
-
- - (void)webViewDidClose:(WKWebView*)webView
- {
- for (UIViewController* vc in windows) {
- if (vc.view == webView) {
- [vc dismissViewControllerAnimated:YES completion:nil];
- [windows removeObject:vc];
- break;
- }
- }
-
- // We do not allow closing the primary WebView
- }
-
-
- @end
|