Delay in loading a HTML string into a UIWebView

自作多情 提交于 2019-12-10 22:18:09

问题


I have two view controllers inside a navigation controller. The 1st view controller has a menu with a button. Pressing this button moves to the 2nd view controller and loads a html string into a UIWebView. Nothing else is being loaded to the webview, just a simple NSString with html code in it. Essentially I'm making a flip card (two views) with the webview as a subview of one of the uiviews.

Here's the code:

containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 280)];
[self.view addSubview:containerView];

frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 280)];
frontView.layer.cornerRadius = 10;
frontView.layer.masksToBounds = YES;
[frontView setBackgroundColor:[UIColor whiteColor]];
backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 280)];
backView.layer.cornerRadius = 10;
backView.layer.masksToBounds = YES;
[backView setBackgroundColor:[UIColor yellowColor]];
[containerView addSubview:frontView];

webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 280, 280)];
webView.scalesPageToFit = YES;
webView.userInteractionEnabled = NO;

NSString *htmlString = @"<head><meta name='viewport' content='width=device-width;'><style type='text/css'>html,body {margin: 0;padding: 0;width: 100%;height: 100%;}html {display: table;}body {display: table-cell;vertical-align: middle;padding: 20px;text-align: center;-webkit-text-size-adjust: none;}</style></head><ruby>金<rt>きん</rt></ruby><ruby>曜<rt>よう</rt></ruby><ruby>日<rt>び</rt></ruby>";

[webView loadHTMLString:htmlString baseURL:nil];
[frontView addSubview:webView];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                      action:@selector(flip)];
[self.view addGestureRecognizer:tap];

}

Issue: I notice that on the first time I press the button to transition to the second view controller that there is a delay in seeing the html string loaded to the webview (it's only about 0.5 - 1 second). This doesn't happen if I go back to the first view controller and hit the button again - this time it's instant.

Any ideas what might be causing this first time delay and how to avoid it?

Thanks


回答1:


Loading the html string causing the delay is a known issue not yet fixed

what you could try is to include an activity indicator and show it hiding the webview and animate it until webview loads completely.This can be achieved by the delegats of UIWebview.And when loading is completed unhide the webview and remove the activity

Use

-(void)webViewDidFinishLoad:(UIWebView *)webView ;
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;



回答2:


Please uncheck the phone numbers detection from UIWebView. It works like charm.




回答3:


Another solution is to switch away from UIWebView to -[UIButton setAttributedTitle:forState:]:

[[NSAttributedString alloc] initWithData:[@"<head><meta ... </ruby>" dataUsingEncoding:NSUTF8StringEncoding]
                                 options:@{
                                           NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding),
                                           NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
                                          }
                      documentAttributes:NULL
                                   error:NULL];

This is still a bit slow but at least the delay is now under your control. Also, The function above appears not to be as feature complete as WebKit; however should be good enough for your purposes.



来源:https://stackoverflow.com/questions/16292616/delay-in-loading-a-html-string-into-a-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!