【问题标题】:UIWebView loadHTMLString shows blank screenUIWebView loadHTMLString 显示空白屏幕
【发布时间】:2023-03-18 05:13:01
【问题描述】:

UIWebView 的 loadHTMLString 有一个奇怪的问题,当我使用内容 HTML 字符串调用 loadHTMLString 时,它只会显示空白视图。 htmlstring 的内容是什么并不重要,它什么也不做。

奇怪的是,几周前它曾经可以工作,当时我在模拟器和设备上对其进行了测试。我的代码如下:

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];

(注意:在这个问题之前,HTML可以在普通的网络浏览器上呈现,所以HTML不是问题)

编辑:添加初始化代码:

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

UIView初始化代码:

- (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;
}

【问题讨论】:

  • 您能否向我们展示您实例化UIWebView 并将其添加到您的视图中的代码?

标签: iphone ios4 uiwebview


【解决方案1】:

经过一番探查,我发现在委托函数中返回 NO

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

将拒绝 loadHTMLString 请求。返回 YES 解决了我的问题。

【讨论】:

  • shouldStartLoadWithRequest 询问 webview 是否应该继续加载页面。返回 NO 会停止加载网页。
  • 就我而言,我想拦截链接以将它们发送到 Safari。为此:if(navigationType == UIWebViewNavigationTypeLinkClicked) return NO; else return YES;.
  • 对于swift:“WKNavigationDelegate 提供:webView:decidePolicyForNavigationAction:decisionHandler:
【解决方案2】:

我也遇到了同样的问题,作为一名 n00b,我发现自己很惊讶当我解决了问题时 没有做

webview = [[UIWebView alloc]init];

但是,由于它已经连接为插座,我只需要致电

[webView loadHTMLString:mystring baseURL:nil];

【讨论】:

  • 这对我帮助很大。我想有了 ARC,您就不必再初始化您拥有的物品了?
【解决方案3】:

UIWebView 的初始化代码来看,您似乎在屏幕外的位置添加了wcbbcPanel。试试这个:

wcbbcPanel = [[WarrantsAndCbbc alloc] initWithFrame:CGRectMake(0, 0, 320, 230)];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-13
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多