Ответ 1
Попробуй использовать
// objC
file = [NSURL fileURLWithPath:filePath];
// swift
file = URL.init(fileURLWithPath: filePath)
вместо
//objC
file = [NSURL URLWithString:filePath];
// swift
file = URL.init(string: filePath)
Я пытаюсь просмотреть документ с помощью 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;
}
Оба метода реализованы в viewcontroller, в котором я хочу представить предварительный просмотр. Я знаю, что жестко получаю данные в объекте urlData
, потому что, когда я помещаю в контрольные точки, консоль говорит, что она содержит 6144 байта, что соответствует размеру загружаемого документа.
Я искал это для спокойствия сейчас, но я не могу понять, как это решить. Я уже пытался вставить объект link
в качестве источника для documentInteractionController, но затем ошибка изменилась на:
'UIDocumentInteractionController: invalid scheme (http). Only the file scheme is supported.'
Любые комментарии о том, как я могу преодолеть эту проблему, будут высоко оценены
Попробуй использовать
// objC
file = [NSURL fileURLWithPath:filePath];
// swift
file = URL.init(fileURLWithPath: filePath)
вместо
//objC
file = [NSURL URLWithString:filePath];
// swift
file = URL.init(string: filePath)