Ios 如何将包含双引号的html字符串加载到UIWebView中

Ios 如何将包含双引号的html字符串加载到UIWebView中,ios,objective-c,uiwebview,Ios,Objective C,Uiwebview,我有一个html字符串,如下所示: <html> <head> <title>some text</title> <link rel="stylesheet" href="http://www.xyz.com/css/main.mobile.css" /> ... 将上面的html字符串放在双引号中。我的目标是将html字符串加载到UIVewView中。提前谢谢 htmlStr = [htmlStr stringByReplacing

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

<html> <head> <title>some text</title> <link rel="stylesheet" href="http://www.xyz.com/css/main.mobile.css" /> ...
将上面的html字符串放在双引号中。我的目标是将html字符串加载到UIVewView中。提前谢谢

htmlStr = [htmlStr stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
然后加载到webwiw

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

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

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

NSString*html=@“一些文本您可以在每个引号前面加一个反斜杠

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

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

// 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);