Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView loadHTMLString shows blank screen

I have a strange problem with UIWebView's loadHTMLString, where it would only display a blank view when I called loadHTMLString with my content HTML string. It does not matter what the content of the htmlstring is, it simply does nothing.

The strange thing is that it used to work a few weeks ago, when I tested it on the simulator and device. My code is below:

NSMutableString *sHtmlBuf = [NSMutableString stringWithString:@"<body style=\"background-color: #000000; color: #FFFFFF; font-family: Helvetica; font-size: 10pt; width: 300px; word-wrap: break-word;\">"];

if ([m_oCallArray count] > 0 || [m_oPutArray count] > 0) {
    [sHtmlBuf appendString:sWarrTitle];

    if ([m_oCallArray count] > 0) {
        NSString *formattedCall = [NSString stringWithFormat:@"%@ %@<br />",sCallTitle,[self arrayToString:m_oCallArray]];
        [sHtmlBuf appendFormat:@"%@ ",formattedCall];
    }

    if ([m_oPutArray count] > 0) {
        NSString *formattedPut = [NSString stringWithFormat:@"%@ %@<br />",sPutsTitle,[self arrayToString:m_oPutArray]];
        [sHtmlBuf appendFormat:@"%@ ",formattedPut];
    }

}

if ([m_oBullArray count] > 0 || [m_oBearArray count] > 0) {
    [sHtmlBuf appendString:sCbbcTitle];

    if ([m_oBullArray count] > 0) {
        NSString *formattedBull = [NSString stringWithFormat:@"%@ %@<br />",sBullTitle,[self arrayToString:m_oBullArray]];
        [sHtmlBuf appendFormat:@"%@ ",formattedBull];
    }

    if ([m_oBearArray count] > 0) {
        NSString *formattedBear = [NSString stringWithFormat:@"%@ %@<br />",sBearTitle,[self arrayToString:m_oBearArray]];
        [sHtmlBuf appendFormat:@"%@ ",formattedBear];
    }

}

if ([m_oOtherArray count] > 0) {
    NSString *formattedOther = [NSString stringWithFormat:@"%@ %@<br />",sOtherTitle,[self arrayToString:m_oOtherArray]];
    [sHtmlBuf appendFormat:@"%@ ",formattedOther];
}

[m_oDataPresentView loadHTMLString:sHtmlBuf baseURL:nil];

(Note: the HTML can render on a regular web browser before this problem, so the HTML is not a problem)

EDIT: Added initialization code:

//Create wcbbc panel
wcbbcPanel = [[WarrantsAndCbbc alloc] initWithFrame:CGRectMake(320, 0, 320, 230)];
[m_oMainContentScrollView addSubview:wcbbcPanel];

UIView initialization code:

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
        CGRect oFrame = frame;
        CGPoint oPositionCoords = CGPointMake(0, 0);
        oFrame.origin = oPositionCoords;

        m_oDataPresentView = [[UIWebView alloc] initWithFrame:oFrame];
        [m_oDataPresentView loadHTMLString:@"<html><body style=\"background-color: #000000; color: #FFFFFF; font-family: Helvetica; font-size: 10pt; width: 300px; word-wrap: break-word;\"></body></html>" baseURL:nil];
        m_oDataPresentView.delegate = self;

        [self addSubview:m_oDataPresentView];
    }
    return self;
}
like image 524
futureelite7 Avatar asked Jan 06 '11 05:01

futureelite7


2 Answers

After a bit of detective work, I found out that returning NO in the delegate function

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

will reject loadHTMLString requests. Returning YES solved my problem.

like image 72
futureelite7 Avatar answered Oct 05 '22 05:10

futureelite7


I just had the same problem, and being a n00b, i found myself surprised that the problem was solved when i DID NOT DO

webview = [[UIWebView alloc]init];

but instead, since it was already wired up as an outlet, i just had to call

[webView loadHTMLString:mystring baseURL:nil];
like image 43
mvishnu Avatar answered Oct 05 '22 06:10

mvishnu