UIDocumentInteractionController "invalid scheme (null)"

Joris416 picture Joris416 · Feb 20, 2015 · Viewed 8.2k times · Source

I'm trying to preview a document with UIDocumentInteractionController. The document is an .xls file which my app downloads from the web. I keep getting this error:

'UIDocumentInteractionController: invalid scheme (null).  Only the file scheme is supported.'

I use the next code:

- (void) webServiceController:(WebServiceController *)webServiceController returnedArray:(NSMutableArray *)array {
    if ([webServiceController.webRequest isEqualToString: @"generateExcelFileForEmployee"]) {
        // start previewing the document at the current section index
        NSString *link = [[NSString stringWithString:array[0]] stringByReplacingOccurrencesOfString:@"AdminFunctions.php" withString:@"AdminFunctions.xls"];
        link = [[[link stringByReplacingOccurrencesOfString:@"\\" withString:@""]stringByReplacingOccurrencesOfString:@"/public/sites/" withString:@"http://"]stringByReplacingOccurrencesOfString:@"\"\\\"" withString:@""];
        NSLog(@"%@", link);
        NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:link]];

        NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"AdminFunctions.xls"];

        BOOL isWriteSuccess = [urlData writeToFile:filePath atomically:YES];
        NSLog(@"%@", filePath);

        if(isWriteSuccess) //success
        {
            file = [NSURL fileURLWithPath:filePath];

            self.fileView = [self setupControllerWithURL:file usingDelegate:self];

            [self.fileView presentPreviewAnimated:YES];
        }
        else
        {
           NSLog(@"file not written");
        }
    }
}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

    UIDocumentInteractionController *interactionController =
    [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;

    return interactionController;
}

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
    return self;
}

Both methods are implemented in the viewcontroller in which I want to present the preview. I know by hard i'm getting the data in the urlData object, because when I put in breakpoints, the console says that it contains 6144 bytes, which is exactly the size of the document to download.

I have been looking for this for quiet a while now, but I can't seem to figure out how to solve this. I already tried putting in the link object as a source for the documentInteractionController, but then the error changes to:

'UIDocumentInteractionController: invalid scheme (http).  Only the file scheme is supported.'

Any comments on how I can overcome this issue would be highly appreciated

Answer

Che picture Che · Feb 20, 2015

Try to use

// objC
file = [NSURL fileURLWithPath:filePath];
// swift
file = URL.init(fileURLWithPath: filePath)

instead of

//objC
file = [NSURL URLWithString:filePath];
// swift
file = URL.init(string: filePath)