アロハ、
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
エラーは次の 2 行に表示されます。
NSData *foundWebResourceData = [[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"];
NSString *foundHTML = [NSString stringWithUTF8String:[foundWebResourceData bytes]];
しかし、それは一貫していません。反復回数と再テスト ボタンを表示する以外に、これが唯一のアクティビティである新しい xcode プロジェクトを作成して、コードを分離しました。ファイルは常に読み込まれ、常に WebResourceData キーを持つ WebMainResource があります。
考えられる手がかりは、代わりにコードを ViewDidLoad に挿入すると、さらに多くの反復で実行されますが、それでも null が検出されることです。ボタン アクションから [self testMe] を呼び出すと、はるかに速くエラーが発生します...理由はわかりません。
私は少し途方に暮れており、それが iOS のバグではなく、単に欠けている基本的なものであることを願っています。どんな助けでも大歓迎です。