【问题标题】:unable to add a local html+css file in iphone webview无法在 iphone webview 中添加本地 html+css 文件
【发布时间】:2011-02-28 16:20:05
【问题描述】:

我正在通过此代码加载本地 html 页面:

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

...它正在完美加载。我已经添加了链接标签来加载项目中添加的本地 css 文件,但它确实按预期加载了页面。

<head>
<link media="only screen and (max-device-width: 480px)" href="/style.css" type= "text/css" rel="stylesheet" />
</head>

但是当我从服务器加载带有 css 的本地 html 页面时,它会完美加载。

<head>
<link href="http://somehitng.com/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>

谁能帮我解决我做错了什么?

【问题讨论】:

    标签: iphone css uiwebview


    【解决方案1】:

    您需要将 Web 视图的基本 URL 设置为 HTML 文件所在的位置,以便本地链接引用起作用。

    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];
    

    【讨论】:

    • 应该是 NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];
    • 糟糕,这是一个愚蠢的错误。我编辑了答案来解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2014-01-19
    • 2014-02-28
    • 2014-07-04
    • 1970-01-01
    • 2022-06-25
    相关资源
    最近更新 更多