【问题标题】:reading local webarchive files -occasionally- returns null WebResourceData读取本地 webarchive 文件 - 偶尔 - 返回 null WebResourceData
【发布时间】:2013-03-25 14:42:24
【问题描述】:

阿罗哈,

我在 iOS 6.1.3 读取 webarchive 文件时遇到了一个问题,其中 -偶尔- WebResourceData 返回 null。

这些是存储在包中的已知良好文件(在 TextEdit 中创建),通常可以正常读取。只是每隔一段时间,他们就不会。

在下面显示的一个简单测试中,我一遍又一遍地读取 3 个不同的文件,直到发现错误。对于 iOS 6.1.3,每次运行测试时,我都会遇到 1 到 200 次迭代之间的错误。我已经在各种设备和模拟器上运行它,结果相同。

    // trying to find out why reading webarchives occasionally
    // returns with empty WebResourceData from known good files.
    //
    - (BOOL) testMe {

        NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithCapacity:100] ;

        int iteration = 1;

        BOOL  ok = TRUE;

        // keep going until we have an error
        while (ok) {

            NSArray *fileNames = @[@"file1",
                                   @"file2",
                                   @"file3" ];

            // LOOP through the webArchives...
            //
            for (NSString *webarchiveName in fileNames) {

                // get the webarchive template
                //
                NSURL *fileURL = [[NSBundle mainBundle] URLForResource:webarchiveName withExtension:@"webarchive"];
                //
                NSData *plistData = [NSData dataWithContentsOfURL:fileURL];
                NSString *error;
                NSPropertyListFormat format;
                //
                plist = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistData
                                                                              mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                                                          format:&format
                                                                                errorDescription:&error];
                // check to see if it loaded
                //
                //
                if(!plist){

                    NSLog(@"ERROR: did not load %@", webarchiveName);

                }else{


                    NSData *foundWebResourceData = [[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"];
                    NSString *foundHTML = [NSString stringWithUTF8String:[foundWebResourceData bytes]];

                    if (foundHTML == NULL) {
                        NSLog(@"      %@ descr = %@", webarchiveName, foundHTML);

                        [errorOutlet setText:[NSString stringWithFormat:@"%@ returned with no content (null) in WebResourceData", webarchiveName]];

                        ok = FALSE;
                    }
                } //---- end of if plist exists

            }  // loop through all 3 files


            [countOutlet setText:[NSString stringWithFormat:@"%d", iteration]];
            ++ iteration;

        }  // keep looping until error

        return ok;

    }  // end of testMe 

错误显示在这两行中:

    NSData *foundWebResourceData = [[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"];
    NSString *foundHTML = [NSString stringWithUTF8String:[foundWebResourceData bytes]];

但这并不一致。我通过创建一个新的 xcode 项目来隔离代码,这是唯一的活动,而不是显示迭代计数和重新测试按钮。文件总是加载,并且总是有一个带有 WebResourceData 键的 WebMainResource。

一个可能的线索是,如果我将代码插入到 ViewDidLoad 中,它会运行更多次迭代,但仍会找到一个空值。从按钮操作调用 [self testMe] 会更快地出现错误...不知道为什么。

我有点不知所措,希望这不是 iOS 错误,而是我只是缺少一些基本的东西。任何帮助将不胜感激。

【问题讨论】:

  • daltonclaybrook 的工作答案,见下文。是的!我会投票给他,但今天才注册……不过,我确实将其作为答案进行了检查。再次感谢!

标签: iphone ios plist webarchive


【解决方案1】:

您可以尝试使用为读取 NSData 而设计的 NSString 初始化程序:

NSString *foundHTML = [[NSString alloc] initWithData:foundWebResourceData encoding:NSUTF8StringEncoding];

【讨论】:

  • 确实有效!知道为什么其他方式没有吗?从这里和网络上的示例中可以找到从 web 存档中获取数据的特殊方法。非常感谢,道尔顿克莱布鲁克!
猜你喜欢
  • 1970-01-01
  • 2014-01-04
  • 2011-06-11
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 2018-06-12
  • 1970-01-01
  • 2020-03-16
相关资源
最近更新 更多