Search code examples
javascriptiosxcodestringuislider

Injecting Javascript into UISlider


I have been battling a problem now for about a week and it's really annoying me. I have embedded html into a uiwebview in Xcode and i want to launch the my javascript function of

ZoomChanger(Enter Value Here)

based on the value of UIslider. I tried this code...

NSString *string = [NSString stringWithFormat:@"ZoomChanger(100,200,300);", slider.value];
[webView stringByEvaluatingJavaScriptFromString:string];

but it only zooms in once and doesn't continue zooming and won't unzoom when uislider is brought back to the beginning. It works fine on android but I can't get it to work on iOS.


Solution

  • You are sending ZoomChanger(100,200,300); each time. I believe you want to send a value from the slider. What is ZoomChanger? I assume it is a javascript function function ZoomChanger(x)....

    You will need something like:

    NSString *string = [NSString stringWithFormat:@"ZoomChanger(%d);", slider.value * 10]; 
    [webView stringByEvaluatingJavaScriptFromString:string];
    

    See this doc.