UIDocumentInteractionController“无效的scheme(null)”
我正在尝试使用UIDocumentInteractionController预览文档。 该文件是我的应用程序从网上下载的.xls文件。 我不断收到这个错误:
'UIDocumentInteractionController: invalid scheme (null). Only the file scheme is supported.'
我使用下面的代码:
- (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; }
这两种方法都是在我想呈现预览的视图控制器中实现的。 我知道硬我得到的数据在urlData
对象,因为当我把断点,控制台说,它包含6144字节,这正是下载文件的大小。
我一直在寻找这个安静的一段时间,但我似乎无法弄清楚如何解决这个问题。 我已经尝试将link
对象作为documentInteractionController的源代码,但随后错误更改为:
'UIDocumentInteractionController: invalid scheme (http). Only the file scheme is supported.'
任何意见,我怎么能克服这个问题将不胜感激
尝试使用
file = [NSURL fileURLWithPath:filePath];
代替
file = [NSURL URLWithString:filePath];