javascript - 如何在 iOS 中为 HTML、CSS 和 Javascript 页面制作包装器

标签 javascript iphone ios css hybrid-mobile-app

我正在构建一个应用程序,我想在其中显示一个本地存储的网页 (HTML+CSS+JS)。我尝试了很多方法,但页面没有响应 java 脚本。这是我试过的代码的一部分

尝试:1

//html
NSString *actualPageMarkupFilePath = [[NSBundle mainBundle] pathForResource:@"inex" ofType:@"html"];
NSData *actualPageMarkupData = [NSData dataWithContentsOfFile:actualPageMarkupFilePath];
NSString *actualPageMarkup = [[NSString alloc]initWithData:actualPageMarkupData encoding:NSASCIIStringEncoding];

// load css styles
NSString *cssPath   = [[NSBundle mainBundle] pathForResource:@"style" ofType:@"css"];
NSData *cssData     = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];

// load js
NSString *jsPath1   = [[NSBundle mainBundle] pathForResource:@"myzepto" ofType:@"js"];
NSData *jsData1     = [NSData dataWithContentsOfFile:jsPath1];
NSString *jsString1 = [[NSString alloc] initWithData:jsData1 encoding:NSASCIIStringEncoding];

NSString *jsPath2   = [[NSBundle mainBundle] pathForResource:@"zepto.min" ofType:@"js"];
NSData *jsData2     = [NSData dataWithContentsOfFile:jsPath2];
NSString *jsString2 = [[NSString alloc] initWithData:jsData2 encoding:NSASCIIStringEncoding];

// compose full html page
NSString *pageContent = [NSString stringWithFormat:@"%@%@%@%@", cssString, jsString1, jsString2, actualPageMarkup];
[self.HTMLWebView loadHTMLString:pageContent baseURL:[NSURL URLWithString:@""]];

尝试:2

NSString *filePath =
[[NSBundle mainBundle] pathForResource:@"inex" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];

if (htmlData) {
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle bundlePath];
    NSString *fullPath = [NSBundle pathForResource:@"inex"
                                            ofType:@"html" inDirectory:path];
    [_HTMLWebView loadRequest:[NSURLRequest requestWithURL:
                          [NSURL fileURLWithPath:fullPath]]];
}

尝试:3

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"inex" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[_HTMLWebView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

尝试:4

NSURL *urlPageFile = [[NSBundle mainBundle] URLForResource:@"inex" withExtension:@"html"];
NSURLRequest *webPageRequest = [[NSURLRequest alloc] initWithURL:urlPageFile];
[_HTMLWebView loadRequest:webPageRequest];
[webPageRequest release];

最佳答案

您应该将包含 html、css 等的目录复制到带有选项的元素
“将元素复制到目标组的文件夹中(如果需要)”
“为任何添加的文件夹创建文件夹引用” enter image description here

并使用以下代码将页面加载到 webview 中:

NSString* path = [[NSBundle mainBundle] pathForResource:@"index.html"
                                                 ofType:nil
                                            inDirectory:@"MyWebSite"];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
[_webView loadRequest:request];

关于javascript - 如何在 iOS 中为 HTML、CSS 和 Javascript 页面制作包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18632728/

相关文章:

javascript - 如何检查结果是否为 __NSCFNumber?

javascript - 与其他软件和程序相比,JavaScript 粒子性能背后的原因

javascript - 类似 Excel 的 PHP 和 AJAX 中没有按钮的表格更新

iphone - 如何在标签栏上绘制符号?

ios - 将两个数组的元素相互相加

ios - 页面 View Controller : Determining "next" and "previous" when using Core Data

javascript - 匹配元素高度 jQuery/HTML

iphone - UIPageControl 子类重写

iphone - ios如何缓存数据

ios - 改变ios背景颜色