Why would webview's loadHTMLString not hit webViewDidStartLoad callback? - iphone

Background: I am developing a news reader type app native for the iPhone which displays many of the news articles as html in UIWebViews. My current goal is to have a css file in my project that can style html that is programmitically meshed up (article + comments, etc). I have done a bunch of little proof of concepts and I know that I can use <link id=\"stylesheet\" rel=\"stylesheet\" href=\"Test.css\" type=\"text/css\" /> as part of the input string to webview's loadHTMLString and achieve the result of styling the html in an expected manner, using the mainbundle's bundlepath as the baseUrl input to that function. Unfortunately, this does not work when I do this in my actual project. Note here that my issue can be reproduced with simple html.
Here's the part where I scratch my head: I can use #"" as the base url and display the unstyled html as expected. But, if I use the bundlepath as the baseUrl instead, the UIWebView goes blank. Investigating further, the webViewDidStartLoad callback never hits it's breakpoint (it does in the #"" case). What would cause this not to fire? Of the 4 callbacks, the only one that fires is the shouldStartLoadWithRequest, which I return NO from. The didFailLoadWithError and the webviewDidFinishLoad are the other two.
My suspicion is that something must be wrong with the mainbundle, but I have no idea what.
I do receive this message in gdb sometimes, although this could be unrelated:
void SendDelegateMessage(NSInvocation*): delegate failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
If you were not using the touch screen for this entire interval (which can prolong this wait), please file a bug.
Code Snippets:
Note: the css link actually has no affect on this issue
UIWebView* contentView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.delegate = self;
NSString *htmlString = #"<html><body>Hello World</body></html>";
NSString *path = [bundle bundlePath];
NSURL *resourceBaseURL = [NSURL fileURLWithPath:path];
[contentView loadHTMLString:htmlString baseURL:resourceBaseURL];
Values at time of loadHTMLString call:
htmlString: "<html><body>Hello World</body></html>"
resourceBaseUrl: file://localhost/Users/U0107552/Library/Application%20Support/iPhone%20Simulator/3.1.3/Applications/D0EE8DA9-B156-47B3-BC53-9A731F813FB1/Test%20App.app/
The one difference that I can see between my working POC and my app is that gdb: po htmlString returns "[the above string] Current Language: auto; currently objective-c" in the POC but just the string in my app.
Sorry for the long-windedness.

Related

UIWebView loading and general performance

I am using UIWebView to display textual content in my app (I store the content in local HTML files that I pack with the app). All together, I have three web views whose content I change dynamically based on user feedback.
Although some might argue that this is not the most accepted way, I find UIWebView very convenient to display formatted text, and modify that text using HTML if necessary. While this works 99% of the time, on occasion, I experience problems that generally fall into one of these categories:
Sometimes, the web view content loads slow and is late a second or so.
The content loads but is not showing. However, as long as, I touch the view (try to scroll or something) the content pops in.
A few times I received memory warnings (usually not long after the app's initial loading) that in no way affected the performance of my app. It logged the memory warning but the app worked like nothing happened.
This is the method I use to load content to my web views:
- (void)loadSimpleDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
Aside from this, the shouldStartLoadWithRequest delegate method is also implemented, always returning a YES.
My question is: Is there a way to improve the reliability/performance of my web views (in particular loading)? Is there something I have overlooked, did wrong, or do not know about?
Some additional info:
I am on iOS6 SDK, use ARC, and do everything programmatically (do not use IB or storyboard).
You have 2 options to check what's going on:
Implement webViewDidStartLoad & webViewDidFinishLoad delegate methods to check why the content isn't showing (may be the content isn't loaded yet).
read the html content to an NSString then use loadHTMLString:baseURL instead of using loadRequest and check if it loads faster.
Hope this could help.

How to show TXT or ASCII file into UIWebView correctly

I'm trying to show TXT file (ASCII) into UIVewView. For example, using the site www.partisani.net/35.txt in Safari on MacBook works fine, Safari on iPhone doesn't. Safari's iPhone shows the file with another layout. Could someone help me, please?
As far as I can tell, the layout of the file on the Mac vs iOS is exactly the same. Are you talking about text wrapping? You can see that on the Mac by resizing the browser.
If you want to handle the line length differently you'll need to do so by setting up scrolling.
UPDATE with more detail:
This "sort of" changes the original content :). Basically, you need to tweak both the contentSize of the webview and embed the text file in some boilerplate HTML to reflow the text rather than have the default viewport width assigned to the text document. The latter I accomplish with something like:
NSURL* url = [NSURL URLWithString:#"http://www.partisani.net/35.txt"];
UIWebView* vw = (UIWebView*)self.view;
vw.delegate = self;
NSData* Data = [NSData dataWithContentsOfURL:url];
NSString* aStr = [[NSString alloc] initWithData:Data encoding:NSASCIIStringEncoding];
NSString* responseStr = [NSString stringWithFormat:
#"<HTML>"
"<head>"
"<title>Text View</title>"
"</head>"
"<BODY>"
"<pre>"
"%#"
"/pre>"
"</BODY>"
"</HTML>",
aStr];
[vw loadHTMLString:responseStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[aStr release];
The vw.delegate = self is important, as you also need to have your controller conform to the UIWebViewDelegate protocol and implement the webViewDidFinishLoad: method. There you can set the scroll width and height of your webview as needed:
- (void) webViewDidFinishLoad:(UIWebView *)webview {
UIScrollView* sview = (UIScrollView*)[[webview subviews] objectAtIndex:0];
sview.contentSize = CGSizeMake(1000, 800);
}
This is an extremely barebones implementation--presumably you would also want logic to calculate the necessary width and height based upon the loaded text rather than use constants as shown here; you'll need some parsing logic associated with the original data for that, but this should get you started.
Setting the scalesPageToFit property to yes might fix your problem.
I'm not sure this is actually something to do with TXT or ASCII, but rather the UIWebview resizing the content.
The file you mentioned loaded into a landscape ipad screen has exactly the same layout as on Mac Safari:
You can change whether or not the UIWebView scales its content with the scalesPageToFit property.

How can I prevent delayed display of my UIWebView

I must include some 'rich text' instructions* preceding a form; and thought adding a UIWebView to the header of the tableview would work. The HTML is a small file in my resources folder, the CSS is in a style tag in the file; so it's all nice and self contained.
The problem is, the view transitions in; then after a small delay the contents of the webview appear. The effect is jarring, and I don't think hiding the view and fading it in when it's ready would be any more desirable.
I'm creating the view with the code below, in my viewDidLoad method.
UIWebView * wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, HEADER_HEIGHT)];
[wv setBackgroundColor:[UIColor clearColor]];
[wv setOpaque:NO];
NSString *path = [[NSBundle mainBundle] pathForResource:#"HeaderMsg" ofType:#"html"];
assert(path != nil);
NSData *htmlData = [NSData dataWithContentsOfFile:path];
[wv loadData:htmlData MIMEType:#"text/html" textEncodingName:#"utf-8" baseURL:nil];
[self.tableView setTableHeaderView:wv];
[wv release];
I found 2 ugly workarounds, but I'm hoping someone has a better solution; since my workarounds make a real mess out of the code:
On previous screen when you make the
UIMYSCREENViewController, call a
[vc
preloadWebViewWithDelegate:self];
method which will make the webview
using the caller as the delegate.
The caller then retains the vc and
waits for the webview to sent it a
webviewDidFinishLoad method, at
which time it can present the view
and release the vc.
The calling view can make the webview,
wait for it to finish, then create the
new view and pass the webview into it.
At any rate, both of those "solutions" make me gag a little, so I'm hoping others have found a better way.
(*The instructions are mostly simple, styled text with some bullet points (no images or overly aggressive styling); but it takes about 14 carefully aligned UILabel views to simulate this without a webview - and is subject to the whims of the customer wanting to change the message.)
I would create another independent model or controller object to create and retain the webview, hopefully at a higher level and before (maybe during app init) displaying the view with the UI that could bring up the webview.
Consider this the same as pre-staging resources for an action game so that they don't have to be loaded during the game loop, which is a common design pattern.
Consider using this
https://github.com/Cocoanetics/DTCoreText/

UIWebView delay in loading local content

I'm displayed a small amount of local content in a UIWebView, roughly 3 paragraphs of text and one 100x100px image. When the UIWebView is pushed onto to the nav controller, it takes about half a second before the content is displayed. This occurs on the device, not in the simulator. Obviously the UIWebView needs to do some work before displaying the content, but is there some way I can do this before the pushed view appears? I have not had any luck doing this myself.
Here is where I'm creating and pushing the view controller which contains the UIWebView:
ArticleViewController* viewController = [[ArticleViewController alloc] init];
viewController.article = article;
[viewController view]; //touch the view so it gets loaded
[viewController loadWebView]; //load the web view content
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
And here is the loadWebView method:
-(void)loadWebView {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString* content = article.content;
NSString *html = [NSString stringWithFormat:#"\
<html><body style='background-color: transparent'><style type=""text/css"">body {font-family: helvetica; color: black; font-size: 10pt; margin: 10px 10px 10px 10px}</style>\
%#<div style='text-align:center'><a href='13443574_9709e4cf37.jpg?photographer=test' style='-webkit-tap-highlight-color:rgba(0,0,0,0);'><img src='13443574_9709e4cf37.jpg' height='160' width='230'></a></div></body></html>", content];
[self.webView loadHTMLString:html baseURL:baseURL];
}
Previously I had [viewController loadWebView] in the viewDidLoad method, but the result seems to be the same. A blank screen when the viewController is pushed, followed by the content loading half a second later.
Any ideas?
Problem Confirmed
I too see a half-second or more delay with a white screen before my UIWebView content appears. This happens only on the first use of a UIWebView during that run of the app. Successive appearances of UIWebView are nearly instantaneous. So it seems to me and other folk that the delay must be due to WebKit libraries needing to load and initialize.
Warm-up WebKit
You cannot eliminate the delay. But you can move that delay to the start of your app, to lessen the annoying effect of "blank screen" to your users. The trick is to load UIWebView with a bogus page off-screen, during your app's launch. Build a minimal HTML5 web page in a hard-coded string. I use a correct and valid HTML5 content to minimize the time taken by UIWebView/WebKit to analyze.
This technique works noticeably well on real hardware (iPhone 3GS), not just the iOS Simulator.
In my app delegates didFinishLaunchingWithOptions method, the bottom of the ARC-compliant code looks like this:
…
[self.window makeKeyAndVisible];
// Performance optimization: Warm up the UIWebView widget and its related WebKit libraries.
// We are choosing the trade-off between (A) a slight delay here during app launch to create and abandon a bogus UIWebView instance, and
// (B) a flash of white and noticeable delay as the UINavigationController slides in from the right the first UIWebView we display during the app run.
UIWebView* bogusWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSString* html = #"<!doctype html><html lang=en><head><meta charset=utf-8><title>blah</title></head><body><p>bogus content</p></body></html>";
[bogusWebView loadHTMLString:html baseURL:nil]; // Load the page.
bogusWebView = nil; // Not really needed, but self-documents that we intend to discard this object.
return YES;
This technique seems to reduce most but not quite all of the approximate half-second delay during the user's first appearance of UIWebView on screen. I conclude that most of the delay is due to WebKit warming up, but there must be some overhead related to graphically presenting a UIWebView on-screen. If so, we cannot eliminate that remaining overhead with off-screen work. But nevertheless, this technique eliminates most of the initial delay. So the user's first impression of my app won't be "slow".
You're going to make the user wait, the only question is: is it before or after the webview appears? If you're set on "before", then you should create the controller, load the web view, but wait to push it until the -webViewDidFinishLoad: delegate method fires. Once you receive that, you can push the view.

iPhone: How to Display Text from UIWebView HTML Document in a UITextView

I have an RSS feed that gets arranged in a UITableView which lets the user select a story that loads in a UIWebView. However, I'd like to stop using the UIWebView and just use a UITextView or UILabel.
This png is what I am trying to do (just display the various text aspects of a news story):
I have tried using:
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];
and assigning the string to a UILabel but it doesn't work from where I am implementing it in webViewDidFinishLoad (--is that not the proper place?). I get a blank textView and normal webView.
If I overlay a UITextView on top of a UIWebView on its own (that is, a webView that just loads one page), the code posted above works displays the text fine. The problem arises when I try to process the RSS feed .
I've been stuck wondering why this doesn't work as it should for a few days now. If you have a better, more efficient way of doing it then placing the code in webViewDidFinishLoad, please let me know! Does it go in my didSelectRowAtIndexPath?
Thank you very much in advance!
I think the you should first log the string returned by :
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];
... in the case of the RSS feed to make sure that you are getting something back. It's possible the RSS page doesn't have the same javascript components and that it returns an empty string.
Once you've confirmed that, then it becomes a simple matter of getting it to display properly in the text view.
If the NSString you want to display is not empty, try to do something like this in the webViewDidFinishLoad method:
[yourUILabel performSelectorOnMainThread:#selector(setText:) withObject:[NSString stringWithFormat:#"bla bla %#", #"more bla"] waitUntilDone:YES];
The main thread of an iphone app is responsible for drawing components, that is why your label doesn't show your text.
You could also try setting setNeedsDisplay: to true
Also, the UILabel will not preserve the HTML format. It will display it as just text.
You could try the following:
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.innerHTML;"];
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML;"];
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerText;"];
You lose out on formatting information with the last line of code.