I'm working to view a local html page inside a WebView and I want to disable going to any external website if the user clicks any button within the page i.e. <a href="www.google.com">
You want to set a policyDelegate for your view. See the docs for WebPolicyDelegate. The specific method you want to implement is:
- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id<WebPolicyDecisionListener>)listener
The simplest thing to do is just call [listener ignore] for everything. That will also prevent any back/forward navigation, reloads, or form submits from working. If you want more control, you can look at the actionInformation dictionary and check the type before calling [listener ignore] or [listener use].
Related
I load up a UIWebView in my app which displays html text and a sign out button.
Is it at all possible that when the user taps the html button it can then change the view? ie: it will go back in navigation of the app to the previous view?
Any examples of this being done?
Can this also be done in java for my Android version?
Edit: If I place an event using JS or something on the button can I then use a listener within the app's web view to go back a view?
What you could do is...
When one of your buttons is pressed, it actually attempts to send the user to a new URL (eg, through a href="", or javascript redirect). But, in your app's code you can use UIWebView's delegate methods to intercept the loading of the URL and instead run some app-based code.
Eg.
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *_url = [NSString stringWithFormat:#"%#", request.URL];
if ([_url rangeOfString:#"?signout"].location != NSNotFound) {
[self userRequestsLogout];
return NO;
}
return YES;
}
Don't forget that UIWebView also has;
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
Hopefully this helps.
– webView:shouldStartLoadWithRequest:navigationType:In web view Click on the hyperlink trigger ,Is there any way How can I do this?
//WebView will automatically load the hyperlink u need not to do anything out there except.
//Inherit <UIWebViewDelegate> in self;
urwebview.delegate=self;
In self implement delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//Its call back receved whenever ur webview is navigated By clicking on hyperlink
}
I'm trying to make an event which will be called (and will excecute objective-c code on iPhone) when specific button is pressed on a website in webView.
The simple way I think is to listen webView's HTTP requests.
Can I do that?
In your HTML, give the URL a special scheme. In this example, the scheme is perform:
<!-- ontouchstart tells WebKit to send us mouse events on a touch platform so we can use :active -->
<button class="button" ontouchstart="" onclick="window.open('perform:MAX')">MAX</button>
(You could use <a href here or other techniques. This example comes from code where using onclick was useful.)
Set your controller as the delegate of the UIWebView. Then implement this delegate method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([[url scheme] isEqualToString:#"perform"])
{
// url.resourceSpecifier will be #"MAX" in this example
// Do something with it.
return NO;
}
return YES;
}
I'm working on an iPad app which is based on a UIWebView: to explain it in the simplest possible terms, the app shows one big interactive webview and in addition it supports custom gestures.
I need to catch events that represent single taps on the webview, but only when the taps have not already been consumed by the webview (i.e. they are not the beginning of a scroll/zoom operation, they are not taps on links, they are not taps that trigger some javascript).
The UIWebView is very greedy with its events, and in my experience it tends not to propagate them, even when they are not consumed. To catch the events, I ended up subclassing the main UIWindow (see http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/). This is working well, but the problem is I'm not able to recognize whether the taps I'm getting have triggered some javascript in the webview or not.
As an additional restriction, I have no control over the javascript that's going to run in the UIWebView or the HTML that it's going to be displayed.
So, the question goes like this: what would be a way to detect all and only the tap events which did not trigger any other action in the UIWebView itself, especially javascript actions?
~
Should you be curious, the source code of the project I'm working on is on GitHub: to find it, just ask Google to point you to Baker Framework.
Do this with JavaScript. First, after your webview finishes loading, attach a javascript click handler to the body to soak up any unused clicks. Have it load a made-up domain as it's action:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *script = #"document.body.onclick = function () { document.location.href = 'http://clickhandler'; }"
[webView stringByEvaluatingJavaScriptFromString:script];
}
Then in your webview delegate, look out for attempts to load that made up domain and intercept them. You now have a way to intercept that javascript click handler with native code and react accordingly:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.host isEqualToString:#"clickhandler"])
{
//handle click
//your logic here
return NO;
}
return YES;
}
I have a webview that displays properly, however the links are entirely inactive. If I try to select a link the selection magnifying glass appears. I've tried overriding
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
but the method will not execute. The class extends UIViewController <UIWebViewDelegate>, and my view is located in a tabbar, but I'm fairly certain everything's connected properly. Any suggestions?
Set the property - Detects link in the UIWebView class to YES.