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];

    }

}


//UIDocumentInteractionControllerDelegate 代理协议

- (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

0 0
原创粉丝点击