Mackey18
Mackey18

Reputation: 2352

UIWebView not calling shouldStartLoadWithRequest on Button with dynamic link

I'm currently accessing webpage which another team has built to cover some functionality in an app (therefore I can't change the webpage) and I've hit something of a small problem. One of the buttons, which is only activated (via Javascript) once certain requisites are fulfilled, refuses to be noticed by the UIWebView when it has been activated and click. That means shouldStartLoadWithRequest doesn't get called and I need it to, to perform some functionality. I was looking at the source and the button doesn't have an HREF initially, which I think may be confusing the UIWebView. Does anyone have any ideas as to how I can make the UIWebView notice this button?

Thanks,
Mike

HTML of Button:

If there isn't a URL: <a class="ts_btn book_btn" href="">Book</a>
If there is a URL: <a class="ts_btn book_btn" href="/bookings/new?date=2013-10-10&amp;bus_id=2&amp;slot_number=9">Book </a>

Upvotes: 3

Views: 1922

Answers (2)

Nick Banks
Nick Banks

Reputation: 111

One approach could be to modify the href in the webview, using the stringByEvaluatingJavaScriptFromString call

NSString *js = @"document.getElementById('mybuttonid').setAttribute('href','http://www.dummylink.com/')";

[webView stringByEvaluatingJavaScriptFromString: js];

Then you can use the

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType

To actually ignore the link and perform the functionality that you require.

Upvotes: 2

user2223516
user2223516

Reputation:

if i am not wrong you want to open link in iPhone Browser from UIWebView. Means When user click on button it will redirect in iPhone Browser. If i am right than please try use the below steps otherwise please comment on below that i am wrong. i will don't mind:

  1. Add UIWebViewDelegate in to your header Class (.h File)
  2. Add Below Method in Your Method Class (.m File)

    #pragma mark - Webview Delegate Methods
    -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType 
    {
          if ( inType == UIWebViewNavigationTypeLinkClicked ) 
          {
               [[UIApplication sharedApplication] openURL:[inRequest URL]];
               return NO;
          }
        return YES;
    }
    

Upvotes: 1

Related Questions