Преглед изворни кода

fix(WKWebview): Fixed non-working iOS alert. (#188)

npm-ued пре 6 година
родитељ
комит
41d9bdcce4
1 измењених фајлова са 83 додато и 0 уклоњено
  1. 83
    0
      ios/RNCWKWebView.m

+ 83
- 0
ios/RNCWKWebView.m Прегледај датотеку

@@ -8,6 +8,7 @@
8 8
 #import "RNCWKWebView.h"
9 9
 #import <React/RCTConvert.h>
10 10
 #import <React/RCTAutoInsetsProtocol.h>
11
+#import <UIKit/UIKit.h>
11 12
 
12 13
 #import "objc/runtime.h"
13 14
 
@@ -339,6 +340,88 @@ static NSString *const MessageHanderName = @"ReactNative";
339 340
 
340 341
 #pragma mark - WKNavigationDelegate methods
341 342
 
343
+/**
344
+* alert
345
+*/
346
+- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler 
347
+{ 
348
+    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
349
+    [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
350
+        completionHandler();
351
+    }]];
352
+    [[self topViewController] presentViewController:alert animated:YES completion:NULL];
353
+
354
+}
355
+
356
+/**
357
+* confirm
358
+*/
359
+- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
360
+    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
361
+    [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
362
+        completionHandler(YES);
363
+    }]];
364
+    [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
365
+        completionHandler(NO);
366
+    }]];
367
+    [[self topViewController] presentViewController:alert animated:YES completion:NULL];
368
+}
369
+
370
+/**
371
+* prompt
372
+*/
373
+- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler{
374
+    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:prompt preferredStyle:UIAlertControllerStyleAlert];
375
+    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
376
+        textField.textColor = [UIColor lightGrayColor];
377
+        textField.placeholder = defaultText;
378
+    }];
379
+    [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
380
+        completionHandler([[alert.textFields lastObject] text]);
381
+    }]];
382
+    [[self topViewController] presentViewController:alert animated:YES completion:NULL];
383
+}
384
+
385
+/**
386
+ * topViewController
387
+ */
388
+-(UIViewController *)topViewController{
389
+   UIViewController *controller = [self topViewControllerWithRootViewController:[self getCurrentWindow].rootViewController];
390
+   return controller;
391
+}
392
+
393
+/**
394
+ * topViewControllerWithRootViewController
395
+ */
396
+-(UIViewController *)topViewControllerWithRootViewController:(UIViewController *)viewController{
397
+  if (viewController==nil) return nil;
398
+  if (viewController.presentedViewController!=nil) {
399
+    return [self topViewControllerWithRootViewController:viewController.presentedViewController];
400
+  } else if ([viewController isKindOfClass:[UITabBarController class]]){
401
+    return [self topViewControllerWithRootViewController:[(UITabBarController *)viewController selectedViewController]];
402
+  } else if ([viewController isKindOfClass:[UINavigationController class]]){
403
+    return [self topViewControllerWithRootViewController:[(UINavigationController *)viewController visibleViewController]];
404
+  } else {
405
+    return viewController;
406
+  }
407
+}
408
+/**
409
+ * getCurrentWindow
410
+ */
411
+-(UIWindow *)getCurrentWindow{
412
+  UIWindow *window = [UIApplication sharedApplication].keyWindow;
413
+  if (window.windowLevel!=UIWindowLevelNormal) {
414
+    for (UIWindow *wid in [UIApplication sharedApplication].windows) {
415
+      if (window.windowLevel==UIWindowLevelNormal) {
416
+        window = wid;
417
+        break;
418
+      }
419
+    }
420
+  }
421
+  return window;
422
+}
423
+
424
+
342 425
 /**
343 426
  * Decides whether to allow or cancel a navigation.
344 427
  * @see https://fburl.com/42r9fxob