Search code examples
javascriptobjective-ccordovauiwebviewcordova-plugins

Cordova Plugin - stringByEvaluatingJavaScriptFromString returning empty value


i seems to be not able to receive any return value from stringByEvaluatingJavaScriptFromString function call at my cordova plugin

Basically I am trying to get the active element in my page, and change my keyboard type based on the 'active element'

I registered UIKeyboardWillShowNotification during the plugin initialise, and fire the javascript when the keyboard will be showing listener.

Here is my code at plugin initialise

-(void)pluginInitialize{
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(onKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

Here is my code when keyboard will show, inside my plugin

- (void)onKeyboardWillShow:(NSNotification *)note{
    NSString *script = @"document.activeElement";

    if ([self.webView isKindOfClass:[UIWebView class]]) {
        NSString * result = [(UIWebView*)self.webView stringByEvaluatingJavaScriptFromString:script];
        NSLog(@"%@",result);
    }
}

The result seems to always return empty string. Is there anything that i did wrong?


Solution

  • manage to solve it by changing to document.activeElement.type instead.

    stringByEvaluatingJavaScriptFromString seems to not able to return object or javascript object.