Web视图未加载html文件

时间:2011-07-07 07:02:21

标签: iphone html uiwebview

我有一些html文件,想要在webView中切换它们。

当我尝试加载任何HTML时,它会给我一些像这样的东西

Load error,The opertaion couldn't be completed.(NSURLErrorDomain error -999.)

我如何解决这个问题。

用于加载htm我正在使用代码行

self.playerPath = [[BundleUtils bundleDirectoryFor:nil] stringByAppendingPathComponent:@"Browser Player files/HTMLplayer/RENE001NL_p001_G02_20_meeleesboek_XCode_pagetr_test_Newgen/"];
        NSString* indexFilePath = [self.playerPath stringByAppendingPathComponent:@"index001.html"];
        NSURL* indexUrl = [NSURL fileURLWithPath:indexFilePath];
        [self.browserView loadRequest:[NSURLRequest requestWithURL:indexUrl]];




    + (NSString *) bundleDirectoryFor:(NSString *) bundleName
    {
        NSString* bundlePath = nil;

        if (bundleName) {
            NSString* basePath = [[[NSBundle mainBundle] resourcePath]
                              stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.bundle", bundleName]];
            NSBundle* bundle = [NSBundle bundleWithPath:basePath];
            bundlePath = [bundle resourcePath];
        } else {
            bundlePath = [[NSBundle mainBundle] resourcePath];
        }

        return bundlePath;
    }

2 个答案:

答案 0 :(得分:1)

编辑:

你会在创建NSURL之前尝试逃避你的路径吗?

 ....
 NSString* encodedParam =  [indexFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 NSURL* indexUrl = [NSURL fileURLWithPath:encodedParam];
 ....

编辑2:

您可以通过在Finder中显示它来检查应用程序二进制文件的内容(右键单击Xco​​de中的产品选项卡),然后选择“显示包内容”。您将看到html的确切位置,并且能够调整您传入的值。

老答案:

我发现您的代码有两个问题:

  1. 您正在使用loadRequest,可以从网络中检索内容,但是(我猜)加载本地文件时效果不错;

  2. 使用完整路径(Browser Player files/HTMLplayer/RENE001NL_p001_G02_20_meeleesboek_XCode_pagetr_test_Newgen/),而iphone应用程序二进制结构是一个平面目录(即,您在Xcode中可能拥有的所有不同文件夹被展平为单个目录)。 / p>

  3. 我建议尝试使用此代码:

    NSString* basePath = [[NSBundle mainBundle] bundlePath];
    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"index001" ofType:@"html"];  
    NSData* myData = [NSData dataWithContentsOfFile:filePath];
    if (myData) {
        [_webView loadData:myData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:[NSURL fileURLWithPath:basePath]];
              ....
    

答案 1 :(得分:0)

错误似乎已取消正在进行的加载请求。

NSURLErrorCancelled
Returned when an asynchronous load is canceled.
A Web Kit framework delegate will receive this error when it performs a cancel operation
on a loading resource. Note that an NSURLConnection or NSURLDownload delegate 
will not receive this error if the download is canceled.
Available in Mac OS X v10.2 and later.
Declared in NSURLError.h.

您的html引用另一个链接或webview委托的方式可能是取消加载操作。很难猜到为什么会发生这种情况。