【问题标题】:How to load html string which contains double quotes into UIWebView如何将包含双引号的html字符串加载到UIWebView中
【发布时间】:2014-04-09 13:30:50
【问题描述】:

我有一个 html 字符串如下所示:

<html> <head> <title>some text</title> <link rel="stylesheet" href="http://www.xyz.com/css/main.mobile.css" /> ...

文本本身包含双引号,所以我无法加载到 NSString 中,因为我不能这样做;

 NSString *html = @"  "

把上面的 html 字符串放在双引号中。我的目标是将 html 字符串加载到 UIVewView 中。提前致谢。

【问题讨论】:

    标签: ios objective-c uiwebview


    【解决方案1】:

    如果您使用的是字符串文字,那么您只需要使用反斜杠转义双引号,如下所示:

    NSString *html = @"<html> <head> <title>some text</title> <link rel=\"stylesheet\" ...";
    

    【讨论】:

      【解决方案2】:

      你可以在每个引号前面加一个反斜杠

      NSString *test = @"lorem\"ipsum\"do...";
      

      或将 HTML 放在一个单独的文件中,然后将其加载到 NSString 中

      // get a reference to our file
      NSString *myPath = [[NSBundle mainBundle]pathForResource:@"myfile" ofType:@"html"];
      
      // read the contents into a string
      NSString *myFile = [[NSString alloc]initWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:nil];
      
      // display our file
      NSLog("Our file contains this: %@", myFile);
      

      【讨论】:

        【解决方案3】:
        htmlStr = [htmlStr stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
        

        然后加载到 webwiew 中

        [self.webView loadHTMLString:htmlStr baseURL:nil];
        

        【讨论】:

          【解决方案4】:

          另一种方法是将字符串中的所有双引号替换为单引号。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-09-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-01-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多