Why is my UIWebView empty in my unit test? - html

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.

Related

html is not getting rendered after web-view finishes content loading

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.
}
}

NSURLSession is not downloading full webpage

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

WebFrame loadData not working?

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.

Black screen appears when viewing HTML5 the second time on UIWebView using iOS6

I'm displaying two HTML5 documents on my iPad application, using a UIWebView on iOS 6.0.1.
One of them has no problems and I can open and close the webview as much as I want.
The other one can be displayed once and then when I close my web view and want to re-open the document again, the webview shows a black screen.
I haven't prepared the HTML5 documents myself and I don't have much knowledge on HTML5, so I can't tell the difference between them that causes this behavior.
Here's how I create my webview and how I load the HTML5:
UIWebView *theWebView = [[UIWebView alloc] init];
theWebView.scalesPageToFit = YES;
theWebView.clearsContextBeforeDrawing = YES;
theWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
//adjust frame and add to view controller's view here
NSURL *url = [NSURL URLWithString:#"http://www.handypdf.net/davsan"];
[theWebView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0]];
Everytime I open an HTML5 document, I re-create the web view. So I don't re-use it.
To fix the problem, I tried cleaning the cache, cookies, etc.. explained on these SO answers, but it didn't work.
Here are the links to the problematic and the not-problematic HTML documents:
This one works ok.
This one doesn't work the second time.
Also, if you want to check these HTML5 pages online here are the links to them:
This one works ok.
This one doesn't work the second time.
This problem does not happen on iOS 5.x. It's also replicable using the iOS simulator.
What would you suggest to fix it?
Thx
Not a satisfying answer, but here's how I progressed with this problem:
I couldn't solve this for a couple of days. Then I deleted all my code and re-wrote the functionality from scratch, and then it was solved. I couldn't understand the exact reason, because I didn't write anything fancy on my second attempt, so unfortunately I can't tell how it's solved :(
Here is what I just did:
Create the web view once, and cache it:
+(UIWebView *)getTheWebView {
if (theWebView) return theWebView;
theWebView = [[UIWebView alloc] init];
theWebView.scalesPageToFit = YES;
theWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
return theWebView;
}
Then, when I want to show the web view:
UIWebView *theWebView = [HTML5Manager getTheWebView];
theWebView.frame = self.view.frame;
[self.view addSubview:theWebView];
And here's how I load the URL:
[theWebView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
This is just it and it works. I know it doesn't make sense but I hope it helps you, too...

How can I pull HTML code into an NSString from an iOS app?

I am trying to pull down the code from an HTML website that has no more than 2 lines on it. The code contains a word that I need to retrieve. Is there a simple way to pull down that code and put it in an NSString?
Further details: I am going to have an app that checks for a word on a page. If that word is what I am looking for, the app will show the text "confirmed". The purpose of the app is to check to see if the page is accessible.
If you need a http library to hit the server try asihttp. Apart from this i need more info of what you are trying to do...
If you just want to check if that website is reachable, you can go with HTTP Success Status Codes.
Using ASIHTTPRequest simplifies communication over the web.
If you still want to evaluate the text on that website, can also just retrieve it using:
[request responseString];
Depending on what you get from the website, it's up to you how to update the UI.
Just change the link between the quotes and it'll work!
-(void) viewDidLoad {
NSString * sFeedURL = [NSString stringWithFormat:#"http://www.google.com/ig/api?weather=,,,270000,960000"];
//RSS Feed URL goes between quotes
NSString * sActualFeed = [NSString stringWithContentsOfURL:[NSURL URLWithString:sFeedURL] encoding:1 error:nil];
NSLog(#"%#", sActualFeed);
}