浏览Stackoverflow但无法知道为什么文件无法打开。我已经在本地设备磁盘上下载了该文件。 UIDocumentInteractionController打开预览视图,但只显示文件名和文件类型。以下条件
1.使用Xcode 6.1.1
2.在Device 5s OS 8.1.2上进行测试
3.以下是我写的代码。
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:
(UIDocumentInteractionController*)controller
{
return self.view;
}
- (IBAction)downloadFile:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";
[HUD show:YES];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fileURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName.text];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
[HUD hide:YES];
NSURL *URL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:fileName.text]];
NSLog(@"URL %@",URL);
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
}