Reputation: 881
Just wondering if there is a way to send keyevents or text to a webview.
I have an app which the user can click a button to perform a specific tasks. Depeneding on the button the user has clicked the textbox inside the webview needs to be populated.
I can handle the button click event and all that but just wanted to find out if I can pass that text to the webview.
I have searched on google but havent found any solutions yet. maybe I am missing something.
Anyway thanks in advance.
Upvotes: 2
Views: 1484
Reputation: 5128
HTML Side:
Assume following is the javascript method which fires upon calling it in your Objective-C method..ie.,native side.
<script type="text/javascript">
var htmlTouch = 0;
//following is the function that you've assigned for a HTML button.
function button1_click()
{
htmlTouch =1; //The reason why i am setting the htmlTpuch=1,is just to identify whether it's native touch or HTML touch.
}
//javascrip function which we are going to call from oObjective-c
function toCallFromiOS()
{
if( htmlTouch == 1 )
{
alert("its a html touch,so pass any values to native code here.");
return 'urText';
}
}
//To reset the touch for next use.
function resetTheHTMLtouch()
{
htmlTouch = 0;
}
</script>
Native Side:
Create UIWebview to load the above html (well i am doing it in local now).
self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"test" ofType:@"html"] isDirectory:NO]]];
Now add the gesture delegate to the whole webview.
UITapGestureRecognizer *tapGestureDown = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)];
tapGestureDown.numberOfTapsRequired = 1;
tapGestureDown.delegate = self;
[self.webView addGestureRecognizer:tapGestureDown];
//handleTapGesture is a native method,in the sense "On detecting if its a native touch,what you want perform?"
-(void)handleTapGesture
{
NSLog(@"Touch is native");
}
Now we are all set.Next step is to implement the delegate called
-shouldRecognizeSimultaneouslyWithGestureRecognizer:==>which returns BOOL value.
On detecting the touch event on the webview,the implemented delegate function gets called.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
If you add the above code just like that,on tapping the webview the above delegate gets called N number of times(sometimes 8,9,13 etc).Only solution is we should be able to know the state of the touch(whether its end or start),to reset the touch event for the next call.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
NSString *javastr=[self.webView stringByEvaluatingJavaScriptFromString:@"toCallFromiOS();"];
NSLog(@"This is return string from javascript==>%@",javastr);
if((otherGestureRecognizer.state==UIGestureRecognizerStateEnded && [javastr hasPrefix:@"urText"]))
{
javastr= [self.webView stringByEvaluatingJavaScriptFromString:@"resetTheHTMLtouch();"];
return NO;
}
return YES;
}
If the javastr
returns any value(text),it's a HTML touch or else its a native touch,"handleTapGesture" gets called.
For more detailed information check out my blog==> Feel the difference between HTML touch and Native touch on UIWebView
Hope this helps you.happy coding...
Upvotes: 3
Reputation: 881
So i completely forgot that is iOS i can simply use javascript. Just in case someone else in the future has the same issue. I found the solution here
http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview
This allows you to input text into the webview's textbox and other places.
Thanks all.
Upvotes: 0