在 UIWebView 中加载本地 HTML 文件(HTML 文件有 css 和链接到它的 jss 文件)iOS

Loading local HTML file in UIWebView (the HTML file has css and jss files linked to it) iOS

谁能告诉我为什么会出现此错误以及如何解决此错误。我正在尝试加载 html 文件,该文件本地保存在文档目录内的文件夹下。 html 文件有 jss 和 css 文件链接到它。请帮我解决这个问题。

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *path = [paths  objectAtIndex:0];

        NSLog(@"Saving");

        NSUserDefaults *prefs = [[NSUserDefaults alloc] init];

        bookid = [NSString stringWithFormat:@"%@",[prefs objectForKey:@"bookid"]];

        NSString *path1 = [NSString stringWithFormat:@"/%@",bookid];

        NSString *dataPath = [path stringByAppendingPathComponent:path1];

        dataPath = [dataPath stringByStandardizingPath];

        NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:NULL];

        for (int count = 0; count < (int)[directoryContent count]; count++)
        {

                NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);

        }

        filePath = [dataPath stringByAppendingPathComponent:@"index.html"];

        NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        [_BookReaderWeb loadHTMLString:htmlString
                        baseURL:[NSURL fileURLWithPath:dataPath]];

这是我得到的错误:

Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x7ffb2a96acf0 {NSUnderlyingError=0x7ffb286b7d20 "The requested URL was not found on this server.", NSErrorFailingURLStringKey=file:///Users/ChanqBro/Library/Developer/CoreSimulator/Devices/A54D2148-91C3-49F4-B94C-CE46DEAEEC58/data/Containers/Data/Application/D08784A9-D5DD-41DE-95C5-DB97BE91BBBC/Library/Caches/3021/blank.html, NSErrorFailingURLKey=file:///Users/ChanqBro/Library/Developer/CoreSimulator/Devices/A54D2148-91C3-49F4-B94C-CE46DEAEEC58/data/Containers/Data/Application/D08784A9-D5DD-41DE-95C5-DB97BE91BBBC/Library/Caches/3021/blank.html, NSLocalizedDescription=The requested URL was not found on this server.}

<html>
<head>
<meta charset="UTF-8">
<title> HELLO </title>

<!-- MONOCLE CORE -->
<script type="text/javascript" src="monocore.js"></script>
<script type="text/javascript" src="monoctrl.js"></script>  
<script type="text/javascript" src="book_data.js"></script>
<script type="text/javascript" src="test.js"></script>


<link rel="stylesheet" type="text/css" href="monocore.css" />
<link rel="stylesheet" type="text/css" href="monoctrl.css" />
<link rel="stylesheet" type="text/css" href="test.css" />

</head>
<body>
<div id="reader"></div>
</body>
</html>

快速检查后,我发现您正在使用 Monocle。在搜索单片眼镜和 blank.html 时,我在 iOS 上发现了一些与单片眼镜有关的问题,这些问题已通过将 HTML 文件添加到名为 blank.html 的空白页面得到解决。

https://github.com/joseph/Monocle/issues/51 or https://github.com/joseph/Monocle/issues/99

So now adding blank.html to your root dir should solve it. (I don't use UIWebView myself, so I'm proceeding on your advice here!)