UIwebview touch event - iphone

I am using a webview and on that web view i have a image. I want to know how i detect touch event of image of webview. I have o open a new view when image is touch.

Depending on the content of the webView, you might have success turning the image into a link, then in your UIWebViewDelegate, implement the webView:shouldStartLoadWithRequest:navigationType: method and check for the image link's URL using the (NSURLRequest *)request parameter.
If you don't have control of the webView's HTML content, but the image does have an ID, you could make it a link using UIWebView's stringByEvaluatingJavaScriptFromString: method to run some javascript and "linkify" the image.

UIWebView inherits from UIView so you could try overriding:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Not sure how much control of the contents of the UIWebView you will have though and if you will be able to figure out the location of the image.

Related

UIWebView scrolling issues

I am using a UIWebView which is updated often using
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
The html string loaded changes to highlight the next word the user has to type in. To scroll the text as the user types, I use:
[webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat: #"window.scrollBy(0, %d);", yScroll]];
,which is called within
-(void) webViewDidFinishLoad:(UIWebView *)webView
inside the web view delegate.
Although this works, the web view is scrolled to the top each and every time it is loaded, and later scrolled to the right position, which causes an undesirable flicker.
Interestingly enough this flicker appears on the device but not on the simulator.
I have tried setting the offset alternatively like:
webView.scrollView.contentOffset = CGPointMake(0, yScroll);
which scrolls the web view but has no effect on the flicker.
I have also overriden
- (void)webViewDidStartLoad:(UIWebView *)webView
and set the hidden property of the web view to YES, made the scroll, and revealed the web view within the
-(void) webViewDidFinishLoad:(UIWebView *)webView
method but to no real effect.
My last resort would be trimming lines from the html string as the user types, loading the updated string every time and avoiding the scroll altogether.
Before I follow that path, does anyone have an idea on how to get the scrolling to work without the "flicker"?
Many thanks

iphone - the order of the touch (tap) event

I have a UIView and a UIWebView.
The UIWebView is a child view of the UIView.
The UIWebView contains a youtube video and is set as to let the video fit to the UIWebView.
I have a UITapGesture associated with the parent UIView for single tap, say, if a user single tap the whole view, it will invoke A.
So, when the UIWebView loads the youtube video, there is a button on top of the video waiting for users to click to play. But now if I click the button, the youtube is played, but also the A is invoked too. This is what I don't want.
How should I solve it?
I thought the touch/tap event should be in a order and if the button is clicked, it should absorb the event and not give the UIView any more.
I also tried to add another UIView under the UIWebView, and attach that gesture to the underlying view. However, it still doesn't work.
How can I do to let the button over the youtube video independent?
thanks
Try add this code in your view's .m file (if you are using UIView, subclass it):
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint p = [self convertPoint:point toView:webView];
if ([webView pointInside:p withEvent:event]) {
return [webView hitTest:p withEvent:event];
}
return [super hitTest:point withEvent:event];
}
Since I did not try this code, tell me if it doesn't work. There are still many ways to deal with the situation. :)

iphone sdk: clickable image in UIWebView

I wanna wrap some text around a UIButton or clickable image in some kind of scrollview.
My ide was to add an UIButton to an UIWebView, but thats not possible?
Is there a way to wrap text around an UIButton in an UIScrollView or UITextView?
Or can I add an image to the UIWebView and handle the clicking somehow?
In the current SDK there is no easy way to wrap text around an image. You could make multiple UILabels and manually cut the strings up into pieces, but that is difficult in itself.
If you make the image a normal anchor, you can handle the opening of the link in the UIWebView delegate callbacks. Return NO and do whatever you want the image to do. You can use a custom scheme to make it easy to distinguish.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] scheme] isEqualToString:#"myscheme"] ) {
// do something
return NO;
}
return YES; // normal link
}
In a UIWebView, you have nearly full access to css and javascript and can do almost anything you could do in Safari. You are not limited to using an anchor tag, that is just the easiest way.

iPhone architect choice for large text flow w/links

I am designing an app which will present large amounts of text that is interspersed with notes and references as clickable images. On a PC I'd use a control that shows HTML, but in the iPhone I am not able to intercept the touches of images and links too well using the UIWeb control.
Should I use a UIScroll and build the text as lables and UIImages perhaps?
Looking for the best way forward in my design phase.
I don't know what your requirements are obviously, but it is possible to capture the click on an link in a UIWebView and take some alternative action. In one of my Apps, I have a UIWebView with one particular link which I want to route differently, while I let all other links open as web pages in the UIWebView as normal. Here's the code snippet from the app which accomplishes this. It is within a UIViewController which loads the UIWebView:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [ request URL ];
if( [[url path] isEqualToString:#"/the_special_link.html"] ) {
// Take some alternative action and then stop the page from loading...
// (code to take some special action goes here)
return NO;
}
else {
return YES;
}
}
This is a delegate method call, so when I set up the UIWebView, which I do programmatically in the Controller loadView method, I set the WebView's delegate to that same Controller:
myWebView.delegate = self;

Figuring if a link is clicked on a webView - iPhone

I am using a custom webview to show a tabbar when user touches on the webview. But when I clicked on a link in a webview, tabbar is shown for a little time and then page loads. I am using this custom webview to detect touch: http://github.com/psychs/iphone-samples/blob/b772b963f2acf18d478b41b44555992fcd8d4f31/WebViewTappingHack/Classes/PSWebView.m
Can I detect if a link clicked or not? Because a link stands on the webview, webview detects the touch and also it loads the page... I want to prevent it.
Thanks in advance.
Using Javascript
Instead of preventing the loading of the page, I would inject Javascript into the webview so that touching the link doesn't do anything.
I've answered a question similar to this not too long ago. Instead of disabling all links (like the other question) just look for the specific link you'd like to disable and remove its href attribute.
Using a UIWebview Delegate
Alternatively, if you want to be able to respond to a user attempting to click the link (perhaps to give them a message), you can set the UIWebview's delegate and implement the webView:shouldStartLoadWithRequest:navigationType: method and return NO if the URL attempting to be loaded is the one you'd like to block.
As an aside, it's usually best practice to maintain a whitelist as opposed to a blacklist for this sort of exclusion. Rather than blocking links you don't want it might be better to block all links, except for the ones you know are safe to navigate to.
UIWebViewNavigationTypeLinkClicked = Tells you when a link has clicked.
Only check you call
self.webView.delegate = self;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
OK, I did a workaround and solved by myself...
My links are at the top & bottom of the page so I got screen coordinates by the following code and if pos.y < some value and pos.y > some value then do not show the menu...
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
NSLog(#"Position of touch: %.3f, %.3f", pos.x, pos.y);
Hope this helps someone...