UIDocumentInteractionController "invalid scheme (null)"
Asked Answered
A

1

22

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

Assizes answered 20/2, 2015 at 10:28 Comment(4)
Have you checked that file has been written successfully or not as you are using same filepath in UIDocumentInteractionController?Eradicate
The only way I have checked this is by nslogging the filepath, but that doesn't really say whether the file was stored does it? Now this is the first time for me to implement a document interaction controller, so could you please explain how to check this?Assizes
I have edited your question with adding vaidation for file written successfully or not. Now replace it and try what happens???Eradicate
Thank you for your suggestion! I implemented it in my app and tested it, but I get the same error, meaning that the file is actually writtenAssizes
B
55

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)
Bergen answered 20/2, 2015 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.