Question

Aloha,

I've come across a problem in iOS 6.1.3 reading webarchive files where -occasionally- the WebResourceData returns a null.

These are known good files (created in TextEdit) stored inside the bundle, and usually read fine. It's just that every so often, they don't.

In a simple test shown below, I read 3 different files over and over until I spot an error. For iOS 6.1.3, I hit an error between 1 and 200 iterations, every time I run the test. I've run this on various devices and the simulator with the same results.

    // 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 

The error shows up in these two lines:

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

but it's not consistent. I've isolated the code by making a new xcode project where this is the only activity, other than displaying the iteration count and a retest button. The files always load, and always have a WebMainResource with a WebResourceData key.

A possible clue is that if I instead insert the code into ViewDidLoad, it runs for many more iterations but still finds a null. Calling [self testMe] from a button action hits an error much faster...not sure why.

I'm a bit at a loss, and hoping that it's not an iOS bug, but rather something basic I'm just missing. Any help would be appreciated.

Était-ce utile?

La solution

You might try using the NSString initializer designed for reading NSData:

NSString *foundHTML = [[NSString alloc] initWithData:foundWebResourceData encoding:NSUTF8StringEncoding];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top