目前开发个文档预览的小功能,其中用到了沙盒机制和UIDocumentInteractionController
iOS中的沙盒可以让平台更加的安全,这也是沙盒给用户带来的最主要好处。不过由于沙盒的严格限制,导致程序之间共享数据比较麻烦。一般在程序间共享文档可以通过UIDocumentInteractionController
UIDocumentInteractionController在iOS 3.2中就已经存在了,使用起来非常灵活,功能也比较强大。它除了支持同设备上app之间的文档分享外,还可以实现文档的预览、打印、发邮件以及复制。
项目中可能的文件类型有以下19种
/*file_type: PDF: 1; DOC: 2; TXT: 3; XLS: 4; HTML: 5; RTF: 6; MHT: 7; RAR: 8; PPT: 9; JPG: 10; DOCX: 11; XLSX: 12; PPTX: 13; EML: 14; ZIP: 15; DOCM: 16; XLSM: 17; XLSB: 18; DOTX: 19 */
其中,PDF,TXT,HTML,DOC,JPG, PNG是可以用UIDocumentInteractionController来打开预览的,而其它格式的文件不能在app上打开,这就有必要提醒用户通过第三方应用来打开。。。接下来是步骤:
1.首先从服务端获取到文件名和文件content数据:根据文件类型确定沙盒目录下的文件名
switch (_fileType) {
case 1:
_pathName = [NSString stringWithFormat:@"%@.pdf",newsDetail.title];
break;
case 2:
_pathName = [NSString stringWithFormat:@"%@.doc",newsDetail.title];
break;
case 3:
_pathName = [NSString stringWithFormat:@"%@.txt",newsDetail.title];
case 4:
_pathName = [NSString stringWithFormat:@"%@.xls",newsDetail.title];
case 5:
_pathName = [NSString stringWithFormat:@"%@.html",newsDetail.title];
case 6:
_pathName = [NSString stringWithFormat:@"%@.rtf",newsDetail.title];
case 7:
_pathName = [NSString stringWithFormat:@"%@.mht",newsDetail.title];
case 8:
_pathName = [NSString stringWithFormat:@"%@.rar",newsDetail.title];
case 9:
_pathName = [NSString stringWithFormat:@"%@.ppt",newsDetail.title];
case 10:
_pathName = [NSString stringWithFormat:@"%@.jpg",newsDetail.title];
case 11:
_pathName = [NSString stringWithFormat:@"%@.docx",newsDetail.title];
case 12:
_pathName = [NSString stringWithFormat:@"%@.xlsx",newsDetail.title];
default:
break;
}
// 创建本地缓存目录下的路径
_paths = [self filePathAtDocumentWithFileName:_pathName];
//获取服务端的文件
NSData *contentData = newsDetail.content;
//对本地路径进行写操作
[contentData writeToFile:_paths atomically:YES];
//获取 沙盒 document路径
- (NSString *)filePathAtDocumentWithFileName:(NSString *)fileName
{
NSString *filePath = [[self cachePath] stringByAppendingPathComponent:fileName];
return filePath;
}
//沙盒路径
-(NSString *)cachePath
{
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
}
//读取document操作:
-(void)openFile{
NSURL *urlStr;
urlStr = [NSURL fileURLWithPath:[self filePathAtDocumentWithFileName:_pathName]];
if (urlStr) {
_doucmentVC = [UIDocumentInteractionController interactionControllerWithURL:urlStr];
_doucmentVC.delegate =self;
}
//如果是pdf文件,则直接进行预览操作
if (_fileType == 1) {
[_doucmentVC presentPreviewAnimated:YES];
[_doucmentVC presentOpenInMenuFromRect:CGRectMake(0, 20, ScreenWidth, 350) inView:self.view animated:YES];
}
else{
//pdf以外的格式则弹出AirDrop让用户选择第三方应用打开文件
[self presentOpenInMenu];
}
}
- (void)presentPreview
{
[self.doucmentVC presentPreviewAnimated:YES];
}
- (void)presentOpenInMenu
{
[self.doucmentVC presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
- (void)presentOptionsMenu
{
[_doucmentVC presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
return self.view.frame;
}
总结