【问题标题】:UIWebView not loading local webpageUIWebView 不加载本地网页
【发布时间】:2013-01-25 02:44:56
【问题描述】:

我有一个超级简单的网页,我只想加载一个本地文件。该文件是/Resources/About.html

这是我的代码:

NSString *urlPath = [[NSBundle mainBundle] URLForResource:@"About" withExtension:@"html"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];

怎么了?

【问题讨论】:

    标签: ios objective-c uiwebview nsbundle


    【解决方案1】:

    URLForResource:withExtension 方法返回 NSURL,而不是 NSString。您可以直接在请求中使用该结果。

    NSURL *URL = [[NSBundle mainBundle] URLForResource:@"About" withExtension:@"html"];
    [webView loadRequest:[NSURLRequest requestWithURL:URL]];
    

    【讨论】:

    【解决方案2】:
    NSString * urlPath = [[NSBundle mainBundle] pathForResource:@"About" ofType:@"html"];
    NSString *content = [NSString urlPath encoding:NSUTF8StringEncoding error:nil];
    [webView content baseURL:[NSURL urlPath]];
    

    【讨论】:

      【解决方案3】:

      我打赌 URLForResource 正在返回 nil(你能确认它不是吗?)。它不会执行递归搜索,并且如果您的“资源”文件夹是实际文件夹(XCode 中的蓝色)而不是组(Xcode 中的黄色),它将失败。尝试使用主包的 resourceURL 并将您的路径附加到它的末尾。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        相关资源
        最近更新 更多