2

My Goal is to send data from iOS device to WkWebView. After researching on how to do this task I get to know that it can be done via executing JavaScript function via WkWebView, therefore, I tried calling just a simple alert function:

@interface UIWebViewController ()  <WKNavigationDelegate, WKUIDelegate>



@end

WKWebView *webView;

-(void)webView:(WKWebView )webView didFinishNavigation:(WKNavigation )navigation{

    NSString* javascriptString = @"alert('Hi');";
    [webView evaluateJavaScript:javascriptString completionHandler:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
    webView.navigationDelegate = self;   

}

Unfortunately I am not able to see any alert with that code?

Can you let me know what kind of silly mistake I am doing in the above code?

Raghav
  • 8,772
  • 6
  • 82
  • 106
  • Possible duplicate of [iOS WKWebView not showing javascript alert() dialog](http://stackoverflow.com/questions/26898941/ios-wkwebview-not-showing-javascript-alert-dialog) – Nef10 May 06 '17 at 19:51
  • The problem is not the calling of the JavaScript, but that `alert()` doesn't show if you don't implement the delegate, see the link above. – Nef10 May 06 '17 at 19:51
  • I have already implemented WKUIDelegate to my controller. – Raghav May 07 '17 at 02:00
  • and does `runJavaScriptAlertPanelWithMessage` get called? – Nef10 May 07 '17 at 02:02
  • I don't have any reference to runJavaScriptAlertPanelWithMessage in my code. – Raghav May 07 '17 at 02:57
  • That is the problem. WKWebView does not show alerts, but will call this method on its `UIDelegate` instead. Please see the link in the first comment on how to implement this method. – Nef10 May 07 '17 at 02:58

0 Answers0