【问题标题】:Unable to load local HTML file in IOS无法在 IOS 中加载本地 HTML 文件
【发布时间】:2014-01-06 12:33:25
【问题描述】:

我正在尝试使用 Xcode 4.6 在 IOS 中加载本地 HTML 文件,但我无法加载它,它在模拟器上没有显示任何内容

我的代码是

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html"     inDirectory:nil];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:htmlString baseURL:nil];
}

谁能告诉我这是什么问题?

【问题讨论】:

  • 你为什么使用过时的 Xcode 版本?
  • 这个网页视图是否被添加到视图层次结构中?
  • 你为什么从文件中获取字符串,而不是加载文件 URL?
  • 您是否尝试在真实设备上加载文件?并非模拟器上的所有内容都与 真实 设备上的一样。

标签: ios


【解决方案1】:

尝试做一些错误检查:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSError *error = nil;
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html" inDirectory:nil];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:error];
    if(htmlString)
        [self.webView loadHTMLString:htmlString baseURL:nil];
    else
        NSLog( @"error in loading string from file %@ - %@", htmlFile, [error localizedDescription]);
}

可能是 .html 文件在您认为应该存在的位置不存在,或者它可能是一个空文件。

另外,利用您可以使用的“NSError”参数。

【讨论】:

    【解决方案2】:

    您可以通过 URL 加载文件

    NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"LocalPage" withExtension:@"html"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:htmlURL]];
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2020-10-15
      • 2018-05-08
      相关资源
      最近更新 更多