【问题标题】:Unable to load html string in UIWebView using loadHTMLString:baseURL in iOS?无法在 iOS 中使用 loadHTMLString:baseURL 在 UIWebView 中加载 html 字符串?
【发布时间】:2013-06-17 19:41:02
【问题描述】:

我正在尝试将 youtube 视频嵌入到我的 iOS 应用程序中。为此,我创建了一个 UIWebView 并尝试从关注 here 加载 Youtube 视频

我已经完成了上述问题的所有答案。即使那样它也不起作用。 我也尝试过加载非常简单的 HTML

 NSString *embedHTML =[NSString stringWithFormat:@"<html><body>Hello World</body></html>"];
 [webView loadHTMLString:embedHTML baseURL:nil];

即便如此,我仍然收到编译错误Parse Issue Expecte ']'

我已尝试清理、退出 XCode 并重新启动它。我不知道,我无法使用该方法。如何为我的UIWebView使用上面的loadHTMLString方法。

PS:请不要将此问题标记为重复。我已经尝试了Stackoverflow 中的所有解决方案。没有任何效果

【问题讨论】:

  • 除了调试什么都做了?记录webview的框架,请在此处添加
  • @LithuT.V.. 我什至无法编译代码。它是一个编译时错误。
  • 评论编译代码的哪一行?
  • 注释[webView loadHTMLString:embedHTML baseURL:nil]; 行完美编译代码
  • 我遇到了同样的错误..你是怎么解决的?

标签: html iphone ios uiwebview


【解决方案1】:
WebView *webDesc = [[UIWebView alloc]initWithFrame:CGRectMake(12, 50, 276, 228)];

NSString *embedHTML = @"<html><head></head><body><p>1. You agree that you will be the technician servicing this work order?.<br>2. You are comfortable with the scope of work on this work order?.<br>3. You understand that if you go to site and fail to do quality repair for  any reason, you will not be paid?.<br>4. You must dress business casual when going on the work order.</p></body></html>";

webDesc.userInteractionEnabled = NO;
webDesc.opaque = NO;
webDesc.backgroundColor = [UIColor clearColor];
[webDesc loadHTMLString: embedHTML baseURL: nil];

【讨论】:

    【解决方案2】:
    - (NSString *)getHTMLContent
    {
        NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"baseline" ofType:@"css"];
    
        NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
        NSString *cssStyle = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
    
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    
        NSString *subtitle = [NSString stringWithFormat:@"%@ | %@", self.article.author, [dateFormatter stringFromDate:self.article.publishedDate]];
    
        NSString *htmlString = [NSString stringWithFormat:@"<html><head><meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'></head><style type=\"text/css\">%@</style><body><div id=\"container\"><h1>%@</h1><p class='subtitle'>%@</p>%@</div></body></html>", cssStyle, self.article.title, subtitle, self.article.content];
    
        return htmlString;
    }
    

    【讨论】:

      【解决方案3】:

      这很简单。您只需要添加一行。试试看:

      NSString *htmlString = @"<html><head></head><body><p>1. You agree that you will be the technician servicing this work order?.<br>2. You are comfortable with the scope of work on this work order?.<br>3. You understand that if you go to site and fail to do quality repair for  any reason, you will not be paid?.<br>4. You must dress business casual when going on the work order.</p></body></html>";
      [WebView loadHTMLString: htmlString baseURL: nil];
      

      【讨论】:

        【解决方案4】:

        如果您希望人们帮助您,您可能需要提供更多代码。我只是以类似的方式使用 webView,它工作正常。

        self.webView = [[UIWebView alloc] initWithFrame:myFrame];
        self.webView.scalesPageToFit = YES;
        [self.webView setBackgroundColor:[UIColor whiteColor]];
        
        //pass the string to the webview
        [self.webView loadHTMLString:[[self.itineraryArray objectAtIndex:indexPath.row] valueForKey:@"body"] baseURL:nil];
        
        //add it to the subview
        [self.view addSubview:self.webView];
        

        能否提供更多信息和代码?

        【讨论】:

        • 这就是我所做的UIWebView *webView = [[UIWebView alloc] initWithFrame:myFrame]; NSString *embedHTML =[NSString stringWithFormat:@"&lt;html&gt;&lt;body&gt;Hello World&lt;/body&gt;&lt;/html&gt;"]; [webView loadHTMLString:embedHTML baseURL:nil]; 就是这样,这就是我所做的。我收到编译时错误。 loadHTMLString 方法本身不起作用。即使一切正常,它仍将Parse Issue Expecte ']' 显示为编译错误
        • 您确定错误来自 webView 吗?如果您注释掉 webView 行,错误就会消失?您是否将 web 视图添加到主视图?
        • 是的。问题出在 webview 上。如果我评论那条线,那么一切正常。即使在添加自身之前,我也收到了错误。
        【解决方案5】:

        这似乎不是 webview 的问题。请在它中断的地方添加代码。错误说您在代码中的某处错过了 ]

        【讨论】:

        • @LithuT.V.. 不,不是因为我离开了]。我什至创建了一个新项目,在那里我写了同一行。我什至可以复制粘贴它。我已经手动输入了所有内容。即使在其他项目中也出现同样的错误:-(
        • 你说的是哪一行??
        • webview 框架好吗?还要检查 webview 是否有有效的内存。连接正确。代码对我来说似乎没问题
        【解决方案6】:

        尝试将 url 直接加载到 webview 中:

         UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(100, 100, 200, 250)];
        
        [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://youtube.com/embed/-0Xa4bHcJu8"]]];
        

        【讨论】:

        • 答案与问题无关
        猜你喜欢
        • 1970-01-01
        • 2016-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-11
        • 2013-05-03
        相关资源
        最近更新 更多