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.
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.
what is wrong with this method?
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
[activityIndicator stopAnimation:self];
}
I want to stop the Circular Progress Indicator (activityIndicator). But there is something wrong with - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame. I am coding for mac osx and not for iOS. I heard something from Delegates, what does that mean?
Check to be sure that your UIWebView has set its delegate. Setting a delegate is basically telling the program who you want to handle events (like taps, gestures, or, in this case, the loading of a webView). Thus when an event is fired, it will inform the delegate and the delegate can process it. Maybe if you post more of your code it would help, but I would check your declaration of the UIWebView in question. Be sure that after you allocate it and initialize it, you set its delegate to self (assuming that this method is in the same class), like so:
UIWebView *myWebView = [[UIWebView alloc] init];
[myWebView setDelegate:self];
If you have not set the delegate, it is firing off events and no one is receiving them to process them. The method you are using is waiting for the specific event sent by any webView. When it is sent an event message it passes, as a parameter, the webView that triggered. In any case, put in a log statement to be sure you are entering the method. That will tell you if it is receiving the event messages.
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
NSLog(#"Did finish loading...");
[activityIndicator stopAnimation:self];
}
NOTE: This is as per iOS experience, but should work for Mac OS as well. Let me know what your log result is, if the method is getting called or not.
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
How Judgment webview page is loaded? Let me tell you the page is loading.
Also, unable to open web pages loaded in Webview, like some of the below URLs.
- (void)awakeFromNib {
NSString *urlString = #"http://www.google.com";
// Insert code here to initialize your application
[[_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
- (IBAction)googleOpen:(id)sender {
[_webView setMainFrameURL:#"http://www.google.com"];
}
- (IBAction)baiduOpen:(id)sender {
[_webView setMainFrameURL:#"http://www.baidu.com"];
}
- (IBAction)yahooOpen:(id)sender {
[_webView setMainFrameURL:#"http://www.yahoo.com"];
}
How can I open a URL with a way to pass parameters?
If you want to know when a website has loaded, link the WebFrameLoadDelegate protocol, and implement webView:didFinishLoadForFrame: to know if the webpage has loaded.
Use webView:didStartProvisionalLoadForFrame: to know if the webpage has start loading.
Check your connection to the internet, and see if you can open the websites from another web browser. If it works, then check if your IBAction's are linked correctly to your buttons.
What do you mean by open a URL with parameters? If you mean by having a textfield which allows users to search keywords on certain websites such as Google, just do http://www.google.com/search?q=%yoursearch, replacing %yoursearch with the users input.
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;
}