UIDocumentInteractionController weird behaviour on iphone with iOS7

660 views Asked by At

I am hitting the wall with a problem for two days now and i would like your help. Before i start i should say that this problem is on iPhone 5 with iOS7 (I have also tested on iPhone 4 with iOS 6 and iPad 2 with iOS 7). This problem began when i tried to upgrade an application that has been on AppStore (iOS4 initially) and tried to make it iOS 7 compatible (supported on iOS6 onwards).

The scenario is pretty simple. I have a view with is a UIDocumentInteractionControllerDelegate. I download a file from a web service save it on the NSTemporaryDirectory and allow the user to Preview or Open In another app using the presentOptionsMenuFromRect. The code simplified is as follow:

I have declared a @property (nonatomic, strong) UIDocumentInteractionController *docController;

@autoreleasepool {
    NSString *fileName = "uniquefilename"
        NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
        fileURL = [NSURL fileURLWithPath:filePath];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if(![fileManager fileExistsAtPath:filePath]){
            NSData *fileData = [NSData dataWithContentsOfURL:[NSURL URLWithString:"theurlofthefile"]];
            NSError *writeError = nil;
            [fileData writeToURL: fileURL options:0 error:&writeError];
            if(writeError) {
                //show error  
            }
        }

docController = [UIDocumentInteractionController interactionControllerWithURL:url];
    docController.delegate = self;
    if (isIpad) {
        [docController presentOptionsMenuFromRect:CGRectMake(location.x + 400,location.y, 100, 100) inView:tableView animated:YES];
    }
    else{
        [docController presentOptionsMenuFromRect:CGRectZero inView:self.tabBarController.view animated:YES];
    }    
}

The problem is that i receive all kind of errors, i repeat the same process all the time and i get different errors, sometimes it works for many times in a row, sometimes it fails from the first go. The errors i receive amongst others which i will add when i get them again are:

  1. * Terminating app due to uncaught exception 'NSGenericException', reason: '* Collection <__NSSetM: 0x16ff61e0> was mutated while being enumerated.'

  2. malloc: * error for object 0x177a56a4: incorrect checksum for freed object - object was probably modified after being freed.

  3. malloc: * error for object 0x1c2c8b50: double free * set a breakpoint in malloc_error_break to debug

When the OptionsMenu is showed successfully i see "AirDrop: collectionView:layout:insetForSectionAtIndex:, orientation: 1, sectionInset: {0, 10, 0, 10}" in the console.

I tried enabling NSZombies and putting a breakpoint for malloc error but did not help me in any way.

Please help me or guide me to the right direction.

Thank you.

1

There are 1 answers

1
Eldin On

Bit late to answer, but hopefully it will help other people.

I had exactly the same issue when saving a file and presenting a UIDocumentInteractionController, it was absolutely random sometimes it would would perfectly 10 times in a row and sometimes it would crash on first try.

It seems to be caused by the file not having finished writing to disk, what fixed it for me was adding a delay before presenting the UIDocumentInteractionController to ensure that the file has finished writing to disk