UIDocumentInteractionController 的使用
来源:互联网 发布:nginx 显示目录 编辑:程序博客网 时间:2024/06/09 18:33
目前开发个文档预览的小功能,其中用到了沙盒机制和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) {
case1:
_pathName = [NSStringstringWithFormat:@"%@.pdf",newsDetail.title];
break;
case2:
_pathName = [NSStringstringWithFormat:@"%@.doc",newsDetail.title];
break;
case3:
_pathName = [NSStringstringWithFormat:@"%@.txt",newsDetail.title];
case4:
_pathName = [NSStringstringWithFormat:@"%@.xls",newsDetail.title];
case5:
_pathName = [NSStringstringWithFormat:@"%@.html",newsDetail.title];
case6:
_pathName = [NSStringstringWithFormat:@"%@.rtf",newsDetail.title];
case7:
_pathName = [NSStringstringWithFormat:@"%@.mht",newsDetail.title];
case8:
_pathName = [NSStringstringWithFormat:@"%@.rar",newsDetail.title];
case9:
_pathName = [NSStringstringWithFormat:@"%@.ppt",newsDetail.title];
case10:
_pathName = [NSStringstringWithFormat:@"%@.jpg",newsDetail.title];
case11:
_pathName = [NSStringstringWithFormat:@"%@.docx",newsDetail.title];
case12:
_pathName = [NSStringstringWithFormat:@"%@.xlsx",newsDetail.title];
default:
break;
}
// 创建本地缓存目录下的路径
_paths = [selffilePathAtDocumentWithFileName:_pathName];
//获取服务端的文件
NSData *contentData = newsDetail.content;
//对本地路径进行写操作
[contentData writeToFile:_pathsatomically:YES];
//获取 沙盒 document路径
- (NSString *)filePathAtDocumentWithFileName:(NSString *)fileName
{
NSString *filePath = [[selfcachePath] stringByAppendingPathComponent:fileName];
return filePath;
}
//沙盒路径
-(NSString *)cachePath
{
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
}
//读取document操作:
-(void)openFile{
NSURL *urlStr;
urlStr = [NSURLfileURLWithPath:[selffilePathAtDocumentWithFileName:_pathName]];
if (urlStr) {
_doucmentVC = [UIDocumentInteractionControllerinteractionControllerWithURL:urlStr];
_doucmentVC.delegate =self;
}
//如果是pdf文件,则直接进行预览操作
if (_fileType ==1) {
[_doucmentVCpresentPreviewAnimated:YES];
[_doucmentVCpresentOpenInMenuFromRect:CGRectMake(0,20, ScreenWidth,350) inView:self.viewanimated:YES];
}
else{
//pdf以外的格式则弹出AirDrop让用户选择第三方应用打开文件
[selfpresentOpenInMenu];
}
}
- (void)presentPreview
{
[self.doucmentVCpresentPreviewAnimated:YES];
}
- (void)presentOpenInMenu
{
[self.doucmentVCpresentOpenInMenuFromRect:self.view.boundsinView:self.viewanimated:YES];
}
- (void)presentOptionsMenu
{
[_doucmentVCpresentOptionsMenuFromRect:self.view.boundsinView:self.viewanimated:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
returnself;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
returnself.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
returnself.view.frame;
}
总结使用UIDocumentInteractionController这个类可以简单地实现app之间文档的预览和打开。这个类的UIDocumentInteractionControllerDelegate协议——这里面有许多delegate
- UIDocumentInteractionController 的使用
- UIDocumentInteractionController 的使用
- UIDocumentInteractionController 的使用
- 本地缓存以及UIDocumentInteractionController的使用
- UIDocumentInteractionController的使用之打开和预览文档
- UIDocumentInteractionController
- (浏览文档)UIDocumentInteractionController打开自己的第三方应用、使用QLPreviewController浏览文档
- iOS 使用UIDocumentInteractionController类打开文件
- UIDocumentInteractionController 调节分享弹窗的size
- 使用第三方APP打开本地文档 (UIDocumentInteractionController)
- 下载文件 ,调用系统的方法(UIDocumentInteractionController) 查看
- UIDocumentInteractionController 第三方应用中打开自己的文件 方法
- iOS UIDocumentInteractionController 设置第三方打开方式的 参数详解
- UIDocumentInteractionController 第三方应用中打开自己的文件 方法
- 8. UIDocumentInteractionController
- iOS 两种预览使用详解(配Demo下载)( Quicklook& UIDocumentInteractionController)
- 在iphone/ipad 上预览阅读服务器端的 PDF not UIDocumentInteractionController
- 利用UIDocumentInteractionController文档预览以及调用iPhone里第三方相关的app打开文档
- vim替换命令
- AngularJs获取对象内容例子
- 使用expect实现shell自动交互
- 堆栈问题
- mysql多对多(转载)
- UIDocumentInteractionController 的使用
- Java集合类总结
- String和StringBuilder的区别
- 单链表的反转(非递归)
- osg粒子
- 相机、分类器、双目、结构光、打印机分辨率的基本知识
- ALAsset,ALAssetsLibrary,ALAssetsgroup常见属性及用法
- ajax上传文件和csv文件读写
- 一个10年的程序员的感叹!