I am using
[webView stringByEvaluatingJavaScriptFromString:#"validate(x);"]
to execute a java script function and I am getting return value that is returned by "validate(x)"
but i want HTML page (response, ie WEB page) that will be displayed after executing java script function.
Is there any way to do so in objective C programming?
You could try calling
NSString *htmlContents = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
Some time after
[webView stringByEvaluatingJavaScriptFromString:#"validate(x)"];
Maybe you could process UIWebView delegate's methods from UIWebViewDelegate when it finishes loading the page,
- (void) webViewDidFinishLoad:(UIWebView *)webView
but I'm not sure if it will work with your JS.
Related
I am not sure of how I would achieve the following functionality and was looking for suggestions. I have an Objective-C iOS app that loads a WebView. The user has a way to download data within the webview running a JS application. When the user triggers this download, I would like to use the downloaded data and execute a method call in the native code.
Looks like you have to use some kind of Hybrid calls. Once Javascript downloads the file, you should invoke a native app call (with a special schema say iosbridgecall:// from Javascript) and check this schema in shouldStartLoadWithRequest,
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] scheme] isEqualToString:#"iosbridgecall"]) {
//here invoke your Native Method, ex:
processDownloadedData();
return NO;
} else {
return YES;
}
}
I'm trying to show a UIWebView with a GIF in it, but only once the GIF has loaded.
I load the GIF as follows:
self.GIFWebView = [[UIWebView alloc] init];
self.GIFWebView.delegate = self;
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
[self.GIFWebView loadHTMLString:html baseURL:nil];
Where post is just an object with some properties such as the URL for the GIF.
Then in webViewDidFinishLoad: I show the web view:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"%f", webView.scrollView.frame.size.width);
NSLog(#"%#", [webView.request.URL absoluteString]);
}
I get "0" and "about:blank" for the NSLogs each time, however.
Why does it not load the GIF properly?
I get "0" and "about:blank" for the NSLogs each time, however.
Not surprising. You're telling the web view to load HTML that you're providing in a string rather than giving it a request. The URL that you're logging is the request URL, and since there's no request, there's no request URL.
Why does it not load the GIF properly?
Possibly because you're misusing the URL object. Look at the code:
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
We can't tell what type post.url is, but it's probably NSURL*. You're probably passing a NSURL into the format string, and that may not produce the result you're looking for. Try passing in a string like [post.url absoluteString] instead of the actual NSURL object.
Also, you might want to log the value of html right after you create it so that you can check the full HTML that you're sending to the web view.
Update: Some additional things to check:
Are you running the code in question on the main thread?
Is the thread's run loop getting time?
Have you tried setting a non-nil base URL?
If the web view's delegate has a -webView:shouldStartLoadWithRequest: method, does it return YES?
What happens if you use a constant string that includes the HTML you want and the hard-coded URL instead of constructing the string with +stringWithFormat:?
Is the test device connected to the network? (Sometimes it's the simplest thing that gets you.)
Does it work correctly if you use a different image? I notice that the URL you're using is for an animated .gif file, try a non-animated .gif or a .jpg image instead.
Update 2: The problem lies in your creation of the web view. Look at the very first line in the code that you showed:
self.GIFWebView = [[UIWebView alloc] init];
That looks okay for a typical object, but -init is not the designated initializer for a view. You should use -initWithFrame: instead. The image loads fine in your sample project when I change the code in your project to use the right initializer:
UIWebView *GIFWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
I'm looking for a possibility to invoke an alert view from a website.
I'm pretty sure that this works somehow because if go through configuring your Apple ID and stuff like that in the App Store you are navigating through webviews and not a native environment (prior to iOS 7!).
Apple uses alert view and action sheets and date picker there so there has to be a way to do so.
I wasn't able to find anything useful on the web nor in the docs.
Cheers
Constantin
To achieve this you can use redirects, set javascript onClick function to some DOM element.
f.e
javascript function callNativeAlert(message) {
window.location = "showAlert://"+message;
}
On UIWebView delegate's you can catch such redirect then show your UIAlertView and ignore loading this page by returning NO
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[request.URL scheme] isEqualToString:#"showAlert"]) {
//TODO: Show your alert here, or execute any native method
NSLog(#"The message is %#", [request.URL host])
...
// Always return NO not to allow `UIWebView` process such links
return NO;
}
....
}
Note: this code has been written from memory
Note2: of course it works if you can modify both javascript and native application
You can use simple JS function alert('message').
I use remote url for contents in my cordova, I use appcache to make it work offline - now the problem is handling the initial load before the appcache gets initialized.
In Android I let the device fallback to the local index.html - this could be informative eg. letting the user know that they have to be online to finalize the install.
// On error show default message page...
public void onReceivedError( int errorCode, String description, String failingUrl)
{
super.loadUrl("file:///android_asset/www/index.html");
return;
}
Question: "How do I accomplish the same in IOS?"
Dont have to write the code for me - hints to files and api would be appreciated
You can you the UIWebViewDelegate methods to detect that your remote content loading has failed to load. For example :
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// here you can either check for the error type or for the url that has failed to load
if([webView.request.url.absoluteString isEqualToString:#"your_remote_url")]
{
NSURL *url = [NSURL urlWithString:#"your_local_url"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
}
Since you simply want to do some error handling, one solution to this would be to do a SubView, and load it from whatever URL you need it from.
You can find a guide on how to do this, here:
http://docs.phonegap.com/en/3.0.0/guide_platforms_ios_webview.md.html#iOS%20WebViews
Of course, this gets called from the native part :)
Hope that helped!
I want to get the URL of my webview. I searched for solutions, the only one was
NSString *myUrl = webView.request.URL.absoluteString ;
This is not working anymore ! it's giving me (null) as a result.
I'm developing for iOS 5.
There is nothing wrong with the absoluteString method, but webView.request is probably nil as well. It seems that the request property of UIWebView is nil until the request has been loaded.
If it's not a problem to wait for the webview to complete the loading, use the delegate method -(void)webViewDidFinishLoad:(UIWebView *)webView to get your URL. If you need it before that, you should get it from another place in your code (for example, where the webview's loadRequest is being called).