UIDocumentInteractionController nil string parameter

516 Views Asked by At

I am using UIDocumentInteractionController to display images, recordings, videos, etc. in this way:

NSURL *URL = [[NSBundle mainBundle] URLForResource:@"2013-08-01 13_20_44 (id)" withExtension:@"mov"];

OR

NSData* dataFile = [dict objectForKey:@"FileData"];
    NSString *urlString = [[NSString alloc] initWithData:dataFile encoding:NSUTF8StringEncoding]; // Or any other appropriate encoding
    NSURL *URL = [[NSURL alloc] initWithString:urlString];

Then:

if (URL) {
        // Initialize Document Interaction Controller
        documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];

        // Configure Document Interaction Controller
        [documentInteractionController setDelegate:self];

        // Preview PDF
        [documentInteractionController presentPreviewAnimated:YES];
    }

By accessing the document from the mainBundle, sometimes it works, sometimes instead I get the following error:

2014-05-23 12:25:54.648 Cleverly[362:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initWithString:relativeToURL:]: nil string parameter'

With the NSData way, I always get it (the NSData is not corrupt, I can open it fine in other ways).

1) Why is this? 2) How can I prevent it at least from crashing? 3) What does the error mean? (why it works for some resources in the mainBundle and not others)?

Thanks in advance for any help

1

There are 1 best solutions below

0
On BEST ANSWER

as crash stated, there might be some cases in the logic which are returning nil urlString, To avoid the crash please add following condition:

if(nil != urlString)
{
    NSURL *URL = [[NSURL alloc] initWithString:urlString];
}

or find the root cause which is returning invalid urlString.