My Problem is I have a webView inside the UIView and UIView have tap gesture and web view class having delegate of UIWebView.
My problem is when i tap on a link it first call tap then shouldStartLoadWithRequest . So i need to prevent calling of code which is written for tap on view.
Any one having any idea............
Edit
code for web view delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
self.isAnyLinkTapped = YES;
RELEASE(webURL);
webURL = [[NSString stringWithFormat:#"%#",[request.URL absoluteString]] retain];
NSArray *urlCoponents = [[request.URL absoluteString] componentsSeparatedByString:#"/"];
if([urlCoponents count]>0)
{
//Some code here
}
}
}
i have add tap gesture like this
_tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
and on tapping below code is invoking.
- (void) tapped:(UITapGestureRecognizer *) recognizer {
touchPointLocationForTap = [recognizer locationInView:recognizer.view];
[(WebViewController*)delegate setIsScreenTouched:YES];
BOOL isThereAnySelection = [self isThereAnyTextSelectionOnWebView];
if (!isThereAnySelection)
[self performSelector:#selector(sendTapEventToWebController) withObject:nil afterDelay:1.2];
}
Please post some code so that everybody can understand..
By the way-
you can identify view which was touched by using this condition
if([[touches view] isKindOfClass:[UIWebView class]]){
//Do whatever you want to do with webview
}else{
//Rest of the code
}
Related
I am pretty sure that I understand how to catch a click on a UIWebView using the webView:shouldStartLoadWithRequest:navigationType: method, but my webView does not even allow me to click the link. I am using:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(12, top, boundsSize.width - 40.0, 400.0f)];
webView.delegate = self;
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.allowsInlineMediaPlayback = YES;
webView.dataDetectorTypes = UIDataDetectorTypeAll;
// add to subview
I am loading in an HTML string rather than loading a URL:
[webView loadHTMLString:bodyHTML baseURL:nil];
Delegate method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"Loaded");
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(urlString);
}
return YES;
}//end
Everytime my webview loads I DO get the "Loaded" in my logs, so I know that the delegate method is getting called. But I can never click on a link within my UIWebView and have anything happen. It does not even look like it is allowing a pressed state on the link. The link is highlighted like a link, just won't allow clicking.
Ideas?
The reason for the UIWebView not responding to touch events is most probably this line:
webView.opaque = NO;
Try setting opaque to YES.
My understanding is that for a view to respond to touch events, that view has to be returned by the call to hitTest:withEvent: during the view hierarchy traversal performed by the event delivery mechanism.
From the hitTest:withEvent: documentation:
This method ignores view objects that are hidden, that have disabled user interaction, or have an alpha level less than 0.01.
Update
Based on new info: if the UIWebView is embedded in a UIScrollView, quote from the Apple docs:
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Please try with following code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked)
{
[[UIApplication sharedApplication]openURL:request.URL];
return NO;
}
return YES;
}
I have a UIWebView which loads a html string on viewDidLoad. It detects if the user clicks a link and display a modal view, but when i close the modal view the UIWebView's html has gone! If i use a FormSheet Modal Style the content stays behind, its only fullscreen modals that cause this. Obviously i can just input the html string again on viewWillAppear which fixes it but this causes makes the content flick back on which i don't want. heres my code:
-(void)viewDidLoad {
[self loadArticleText];
...
}
-(void)loadArticleText {
NSString *htmlHead = #"<html><head>...";
NSString *htmlFoot = #"</body></html>";
NSString *htmlContent = [self.articleData valueForKey:#"fulltext"];
NSString *html = [[NSString alloc] initWithFormat:#"%#%#%#", htmlHead, htmlContent, htmlFoot];
[self.articleView loadHTMLString:html baseURL:nil];
[html release];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
ImageViewer *imageView = [[[ImageViewer alloc] initWithNibName:#"ImageViewer" bundle:nil] autorelease];
imageView.imageURL = URL;
[self presentModalViewController:imageView animated:YES];
return NO;
} else {
return YES;
}
}
If i change 'return NO;' to 'return YES;' the webView contents stays, like it should, but obviously it loads the image.
Any help??
Thanks
[self presentModalViewController:ImageView animated:YES];
It looks like you are making a class call rather than an instance call on ImageView.
When a user clicks a link in the present UIWebView a new view is pushed onto the navigationController's stack that contains a UIWebView. I'd like to pass the URL that was touched to this new UIWebView. How do I go about doing this?
In your UIWebView delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if( navigationType == UIWebViewNavigationTypeLinkClicked ) {
YourWebViewController* vc = [[YourWebViewController alloc] initWithURL:[request URL]];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
return NO;
}
return YES;
}
Then you just need to implement the initializer in your custom viewcontroller:
- (id)initWithURL:(NSURL*)url {
self = [super init];
if( self ) {
myURL = [url retain];
}
return self;
}
Then load it at an appropriate time, like viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
[webView loadRequest:[NSURLRequest requestWithURL:myURL]];
}
I have a view and a button. When the button is pushed, I present a modal view controller that has a uiwebview, and a navigation bar at the top. The hardcoded uiwebview shows a login prompt. Once the user goes in and enters the correct login credentials and hits GO on the keyboard, I not only need it to authenticate me into the server, but also dismiss the modal view controller.
Note the text field I enter data into is part of the website loaded in uiwebview...
When I hit enter now, the web page authenticates, and everything is peachy, but I just need a way to tie in a
[self dismissModalViewControllerAnimated:YES];
command when they hit GO...any thoughts?
Thanks
EDIT
Here is the code that I am implementing in the loginView:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString: #"http://website"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
[webView loadRequest: request];
[request release];
[url release];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[self dismissModalViewControllerAnimated:YES];
return YES;
}
-(IBAction) done{
[self dismissModalViewControllerAnimated:YES];
}
First determine the URL the webpage takes you to on successful login, let's say it's http://website/logedin. Implement UIWebViewDelegate, and detect when a request is made to that address:
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[request.URL absoluteString] isEqualToString:#"http://website/logedin"]) {
[self dismissModalViewControllerAnimated:YES];
return NO;
}
return YES;
}
you can try with below sample code;
#interface WebViewController : UIViewController {
}
UIWebViewDelegate having few delegate methods, you have to implement it in WebViewController.m (for eg).
-(void) webViewDidStartLoad:(UIWebView *)webView{
}
-(void) webViewDidFinishLoad:(UIWebView *)webView{
-(void) webViewDidFinishLoad:(UIWebView *)webView
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
}
Before this, you have to set the delegate for UIWebView from Interface Builder.
I have a view controller and I am intercepting the touches on links within the web views it manages.
My main view controller has this method.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
//I can see this request come in upon a touch
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(#"raw: %#", urlString);
//do some stuff (like figure out what capturedFilename is)
ExplainViewController *explanation = [[ExplainViewController alloc] initWithNibName:#"ExplainViewController" bundle:nil file:capturedFilename];
[self.navigationController presentModalViewController:explanation animated:YES];
}
And this loads a modal view properly.
ExplainViewController has a webView itself. When a user touches a link within ExplainViewConroller I'd like to handle that request too (and present another modal view).
ExplainViewController has this but I get no log activity from either method (the following nor the previous):
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(#"raw: %#", urlString);
}
I want to know where this link touch request is going and how I can intercept it.
Both mentioned view controllers are using this in their .h
<UIWebViewDelegate>
You mention that both view controllers conform to the UIWebViewDelegate protocol, but is the delegate outlet of the UIWebView in the nib for ExplainViewController.h set to an instance of ExplainViewController. It will not call the method unless the UIWebView delegate property is set.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
BrowserController *browserController = [[BrowserController alloc] initWithUrl:request.URL.absoluteString];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browserController];
[self.navigationController presentModalViewController: navController animated:YES];
[browserController release];
[navController release];
return NO;
}
return YES;
}