I have to render html into web-view, In some cases it renders nothing on webview when
-invalid html received from server or html contains flash,.swf and other unsuported media types for iOS.
Webview's webViewDidFinishLoad is getting called in this case also, so I am unable to get this using webview's delegate methods.
To detect these cases I am taking points colors on webview diagonally and when I get alpha 1, it means html render successfully else its a blank.
I am using UIView+ColorOfPoint for getting color at point.
Is there any best way to achieve the same. Please help if you any any better solution.
Thanks
Try to implement following code.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[webView loadHTMLString:[NSString stringWithFormat:#"%#", error] baseURL:nil];
if (error)
{
// implement your code.
}
}
Related
I'd wanted to know how or whether I can type in something in a textField on a website from my iPhone application code. So I want to go to a website where is one textField in the middle and there I would like to type in a specific string. How can I do that in Swift (or Objective-C - I'll figure out how it works in Swift then)? I would really much appreciate any help :)
You can inject JavaScript into an UIWebView by calling the method stringByEvaluatingJavaScriptFromString. You write a piece of JavaScript where you select the input field and modify it's value attribute.
The method is described here:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/index.html#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString:
There actually is an article on the web that explains how to do just what you need:
http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview/
The following is the part you should use (you just have to change the ID in the DOM selector):
[webView stringByEvaluatingJavaScriptFromString:#"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function myFunction() { "
"var field = document.getElementById('field_3');"
"field.value='Calling function - OK';"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"];
[webView stringByEvaluatingJavaScriptFromString:#"myFunction();"];
You can inject javascript from your delegate. For instance, in Obj-C (I don't know swift good enough yet) :
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[_webView stringByEvaluatingJavaScriptFromString:#"doSomeStuff()"];
}
In your case, you'll want to manipulate the DOM to add some text in your textfield - something like this :
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString * javascript_code = #"document.querySelector('input[name=your_input_name]').value = 'Foobaz'";
[_webView stringByEvaluatingJavaScriptFromString:javascript_code];
}
Beware of querySelector though, I'm not sure about its availability in iOS's webview.
I'm currently attempting to download a webpage using a NSURLSession in order to retrieve a status update on the download progress. Unfortunately, after downloading the webpage, when I go to load the webpage, there are visual issues (missing images, missing javascript, missing styles, etc), leaving the webpage looking broken and in complete. When loading directly to the webView, everything loads correctly. I would like to know if there is a way (or anything I'm missing) to download ALL aspects of the webpage and load them up so I may load the full webpage and display a loading bar while the page loads.
- (void)loadRequest:(NSURLRequest *)request
{
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
self.urlSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];
self.downloadTask = [self.urlSession downloadTaskWithRequest:request];
[self.downloadTask resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL
{
[_webView loadData:[NSData dataWithContentsOfURL:downloadURL] MIMEType:downloadTask.response.MIMEType textEncodingName:downloadTask.response.textEncodingName baseURL:nil];
}
I was having similar problems (missing images etc.). You need to provide a baseURL. Try:
[_webView loadData:[NSData dataWithContentsOfURL:downloadURL] MIMEType:downloadTask.response.MIMEType textEncodingName:downloadTask.response.textEncodingName baseURL:downloadTask.response.URL];
In order to display a loading bar you have to make some kind of workaround, because you are not able to know the progress of the resources that are being downloaded.
Many apps use tricks, to simulate that there is a loading ongoing.
Here is a simple example to ilustrate the idea:
Display loading bar with progress 30% for 1 second.
If the request did not finish, display progress 90% until the progress finishes
When the request finishes, animate progress to 100%.
You can have a look on this open source library to see how it's done. You can do it in a similar way.
https://github.com/ninjinkun/NJKWebViewProgress
I'm trying to make a WebView load a page from HTML code I have stored as an NSData. I get a blank page when I try to do this. Is there anything wrong with what I'm doing when I load the page? If not, I need to look elsewhere in my program.
if (essence.html){ //essence.html is an NSData
NSLog(#"Inserting HTML code into browser window: %#", [[NSString alloc] initWithData:essence.html encoding: NSUTF8StringEncoding]);
[webView.mainFrame loadData:essence.html MIMEType: #"text/html" textEncodingName: #"utf-8" baseURL:nil]; //webView is a WebView
}
I created the conditions so essence.html contains HTML code from the page http://kathleenmelian.com/test.html (which just says "hello"). The NSLog prints this when the above code runs:
Inserting HTML code into browser window: <html><head></head><body>hello</body></html>
So essence.html definitely contains valid code that a browser should be able to load.
You could use
[webView loadHTMLString:
[[NSString alloc] initWithData:essence.html encoding:NSUTF8StringEncoding]
baseURL:nil];
The other idea would be to replace "utf-8" with "UTF-8", which in some cases is known to make a difference (not sure about UIWebView).
Sorry to bother you guys. I fixed some other bug in my program's model, and that somehow fixed THIS problem as well. I don't know how. Crisis averted.
i want to take a normal html page (a page from tf2 wiki) and convert it into a view.
For example it should take a text and put it into a label or a Image into an UIImageView.
Is there any Framework to do something like that or should i convert the site on a webserver
to make it easier for the app at runtime to render ?
Hope you understand, what i want to do.
Tim
there could be different ways depending on your scenario, you could load them in UIWebView within IOS, If you don't want show the UIWebView then you can get the html from it ans use it however you want. or other way you could transfer the HTML as json data to IOS and then show that data via UIView, UILabel or anything else. otherwise as suggested by Zakaria, you could use PhoneGap.
UPDATE with sample code for sending a request and getting its contents if you don't want to use UIWebView
//your html page with url
NSURL *url = [NSURL URLWithString:#"www.google.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0f];
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length]>0 && error==nil) {
NSString *html=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"this string contains all html page you have \n %#",html);
}
}
];
Not sure if i understand the question properly, but Cordova lets you develope iOS apps in html/css/js.
Check their website: http://cordova.apache.org/
If your page is a static one, then use PhoneGap : It will embed your HTML+JS+CSS in an iOS app and will be loaded in a webview.
But there is no framework that will allow you to convert your HTML elements into native ones.
I'm putting app-generated content into a UIWebView, and trying to test that I'm doing it correctly. Here's the HTML string I'm putting into the web view:
#"<html><head></head><body><p>Come, let me clutch thee. I have thee not, and yet I see thee still. Art thou not, fatal vision, sensible to feeling as to sight?</p></body></html>"
And it gets into the web view thus:
[self.webView loadHTMLString: [self HTMLStringForSnippet: model.body] baseURL: nil];
where model.body contains just the <p/> element, and -HTMLStringForSnippet: wraps it into well-formed HTML. In my test, I rely on the answers to this question to retrieve the HTML content of the body via JavaScript:
- (void)testCorrectHTMLLoadedInWebView {
UIWebView *bodyView = controller.webView;
NSString *bodyHTML = [bodyView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
STAssertEqualObjects(bodyHTML, model.body, #"Model body should be used for the web view content");
}
While I can see by stepping through the code that the UIWebView is created correctly and is passed the correct HTML in -loadHTMLString:baseURL:, in the test bodyHTML is empty. So what do I have to do to see the actual content I expect and previously passed to the web view?
It takes a noticeable time for the UIWebView to render therefore the problem may be that you access the content before it is fully loaded.
Hook on UIWebViewDelegate's webViewDidFinishLoad: method to make sure the content is ready.
Update:
Maybe WebViewJavascriptBridge will bring some help.