I am loading a jqueryMobile html file into webview and webViewDidFinishLoad event is properly triggered. However, when you select a navigation button of loaded jqMb file that loads another html file content, event is not fired! how to capture it? Thank you
solved using fake javascript call, widow.location = "localFuction".
Then, - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest is triggered and if ([[[request URL] absoluteString] hasPrefix:#"localFunction"]) { Return NO;
Hope this helps
Related
I am working on a project using Objective C on macOS. The project is Document-Base Application. Each window contains a WebView. I want to implement copy: cut: and paste: where the menu items for these actions should be enabled/disabled according to the object selected inside the WebView. I started a new Document-Base Application and added the following code inside AppDelegate.m:
- (void)copy:(id)sender {
NSLog(#"%# %s", self, __func__);
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
NSString *title = [menuItem title];
NSLog(#"%#",title);
return YES;
}
Before I add the WebView to the Document.xib file, everything is working fine and both copy and validateMenuItem functions are called normally. When I add WebView to the Document.xib file, these function stop working. I tried many things (such as override the WebView class) without any success. I know WebView is deprecated, but using WKWebView will cause other issues.
The WebHTMLView inside the WebView is the target for the copy: action. The editingDelegate of the WebView can intercept the copy: action. Implement
- (BOOL)webView:(WebView *)webView doCommandBySelector:(SEL)selector;
The UIDelegate of the WebView can validate the Copy menu item.
- (BOOL)webView:(WebView *)webView validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item defaultValidation:(BOOL)defaultValidation;
I need to inject some js code on my webpage being loaded in my UIWebView before any other script is executed. Unfortunately it seems that HTML document.onload is executed before objective-c webViewDidFinishLoad callback.
Check this test code out:
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
[webView loadHTMLString:#"<body onload=\"alert('this is from html')\"></body>" baseURL:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:#"alert('this is from obj-c')"];
}
Message "this is from html" is always shown before message "this is from obj-c".
I need to get the other way around considering that I cannot control my html page, so I have to assume there's a document.onload function being called.
...ok found...
I need to use webViewDidStartLoad instead of webViewDidFinishLoad.
Surprisingly to me, jscode injected during webViewDidStartLoad callback is available to the loaded HTML page. This means to me that the UIWebView javascript engine is not reset after webViewDidStartLoad which is fine to me.
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.
how can I check when user clicks and page has loaded? Delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
Not working..
When the user taps a link, the UIWebView's delegate is sent the message webView:shouldStartLoadWithRequest:navigationType: and when the content is done loading, it sends the message webViewDidFinishLoad:.
Make sure you've set the delegate property on the web view.
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;
}