【问题标题】:Load html file from a framework is this possible?从框架加载html文件这可能吗?
【发布时间】:2016-02-07 10:26:33
【问题描述】:

您好,我想从我的项目中加载 html 和 .js 文件。这些是我通过框架加载 html 文件的以下步骤,但我无法点击 html 文件。

接下来的步骤。

创建了一个框架,其中包含 webview 作为子视图。

创建 Resource.bundle 并将我所有的 html、.js 文件和图像添加到包中。

当我将此框架添加到我的项目中并尝试从包中访问图像时,我无法访问。 当我尝试点击 html 文件时,我无法在我的 webview 中加载 html 文件。

    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];
    NSString *htmlFile = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"simple" ofType:@"html"];

    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    NSURL *url=[[NSBundle mainBundle] bundleURL];
    [webview loadHTMLString:htmlString baseURL:url];
    [self addSubview:webview];

谁能告诉我为什么这行不通,这是正确的做法吗?

【问题讨论】:

  • 试试这个 [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"] isDirectory:NO]]];
  • 您确认资源确实在捆绑包中吗?检查您的构建产品并使用 Finder 打开捆绑包。
  • @Joris Kluivers 是的资源在捆绑包中......

标签: html ios objective-c iphone xcode


【解决方案1】:

我使用 index.html 创建了一个捆绑包,它使用简单的警报调用 js,使用此代码我可以设置警报

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSString *htmlFile = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"index" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];

[_web loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];

html 在本地工作? webview 初始化了吗?

【讨论】:

  • 是的,html 在本地工作,webview 已初始化...我尝试了上面的代码,但它不工作...
猜你喜欢
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2013-12-29
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多