文件查看 UIDocumentInteractionController

UIDocumentInteractionController是从iOS 3.2的SDK开始支持的,它是直接继承的NSObject,而不是我们想象的UIViewController。

基础操作

在当前“视图控制器”里面:

NSString *cachePath = [[NSBundle mainBundle] pathForResource:@"民族" ofType:@"xlsx"]; 
NSURL * pathUrl = [NSURL fileURLWithPath:cachePath];//文件路径
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:pathUrl];
documentController.delegate = self;

[documentController presentPreviewAnimated:YES];//跳转到查看页面



#pragma mark - UIDocumentInteractionControllerDelegate
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}
-(UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    return self.view;
}
-(CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    return self.view.bounds;
}
//-(void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
//    NSLog(@"%@",controller);
//}
//-(void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
//    NSLog(@"%@",controller);
//}
//-(void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
//    NSLog(@"%@",controller);
//}




🌰“支持类型”的展示例子:


播放音乐
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//文件路径
NSString *docDir_Str = [paths objectAtIndex:0];
NSString *doc_PathStr = [docDir_Str stringByAppendingString:@"/localFile"];

//直接打开
fullFileNameStr = @"心跳(原味版)-于文文.mp3";
NSString * filePathStr = [doc_PathStr stringByAppendingPathComponent:fullFileNameStr];
NSURL * fileUrl = [NSURL fileURLWithPath:filePathStr];
UIDocumentInteractionController *documentController =  [UIDocumentInteractionController interactionControllerWithURL:fileUrl];
documentController.delegate = self;
//跳转到文档查看页面
[documentController presentPreviewAnimated:YES];

播放音乐,效果:



播放视频
fullFileNameStr = @"11111.mp4";

播放视频,效果:



文档查看
fullFileNameStr = @"民族.xlsx";

文档查看,效果:


图片查看
fullFileNameStr = @"chx.jpg";

图片查看,效果:

其文件查看非常强大,支持很多类型!




🌰“文档操作”例子
在自己App里面,对文档 进行操作:(可自动检测可进行分享的App)






打开 第三方App里面的文档

当我们需要打开 在 第三方App里面的文档时,可以在“info.plist”中添加键值对来搞定这个问题:


Source Code格式

<key>CFBundleDocumentTypes</key>
<array>
   <dict>
       <key>CFBundleTypeName</key>          <!--类型名-->
       <string>pdf</string>                 <!--随意取名字-->
       <key>CFBundleTypeIconFiles</key>     <!--类型文件的icon图-->
       <array>
           <string>MySmallIcon.png</string>
           <string>MyLargeIcon.png</string>
       </array>
       <key>LSItemContentTypes</key>        <!--所关联的UTI格式,一个类型名可以对应多个UTI-->
       <array>
           <string>com.adobe.pdf</string>
       </array>
       <key>LSHandlerRank</key>             <!--是不是app所拥有的类型,Default:不是,Owner:是-->
       <string>Owner</string>
   </dict>
</array>


在以下链接中,有文件后缀UTI(统一类型标识符) 的对应表:
https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html


“info.plist”修改实例:(“Property List”格式)


如何导入文档:

当我们从其他的App,用“readerApp”打开文档时,会调用“readerApp”的方法!
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { }

其中,url对应的就是该文档位置
实际在此时,该文档已经复制到“readerApp”的“document/inbox”文件夹下。
我们需要将该文档移动到其他位置,因为每次其他应用调用“readerApp”打开该文档时,都会在inbox文件夹下复制一份文件(不考虑该文件是否已经存在),如果多次使用,那么inbox将会占用大量的存储空间。

总结:记得在移动了文档后,要删除掉inbox路径里面的内容












自己的文档阅读APP 书写🌰:

1.在“info.plist”里面,添加可以支持的所有类型:(“Source Code格式:)

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>com.myapp.common-data</string>
        <key>LSItemContentTypes</key>
        <array>        <!--罗列了 支持的类型-->
            <string>com.microsoft.powerpoint.ppt</string>
            <string>public.item</string>
            <string>com.microsoft.word.doc</string>
            <string>com.adobe.pdf</string>
            <string>com.microsoft.excel.xls</string>
            <string>public.image</string>
            <string>public.content</string>
            <string>public.composite-content</string>
            <string>public.archive</string>
            <string>public.audio</string>
            <string>public.movie</string>
            <string>public.text</string>
            <string>public.data</string>
        </array>
    </dict>
</array>

这样,其他应用程序的文档在使用第三方打开的时候,就可以看到你的应用程序了。

对应的“Property List格式:


文档查看操作

点击该文档,打开
对文档,进行操作


添加可支持的类型之前 添加可支持的类型之后
无 自己的App,可供选择
有 自己的App,可供选择




反复导入文档:

每次导入,都会在inbox文件夹下复制一份文件。
所以出现了:名称后面文档 命名为“-3”的情况




解决操作

导入文档后,不管是否保存在本地,都要删除Inbox 面的文件

//fileNameStr:文件名  (导入所产生文档所在的Url 获取来的)
NSString * fileNameStr = [_docUrl lastPathComponent];

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * docDir_Str = [paths objectAtIndex:0];//沙盒文档路径

//删除文件的路径
NSString * deleteDocPath = [[docDir_Str stringByAppendingPathComponent:@"Inbox"] stringByAppendingPathComponent:fileNameStr];
//删除Inbox里面的文件
[[NSFileManager defaultManager] removeItemAtPath:deleteDocPath error:nil]; 




2.文档导入操作

全局变量:NSURL * _docUrl;//文档Url

/** 文档导入 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {  
      if (self.window) { 
          if (url) {
              _docUrl = url;
              NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
              NSString *docDir_Str = [paths objectAtIndex:0];
              NSString *pathStr = [docDir_Str stringByAppendingString:@"/localFile"];
              NSFileManager * fileManager = [NSFileManager defaultManager];
              if(![fileManager fileExistsAtPath:pathStr]){
                  //如果不存在,则说明是第一次运行这个程序,那么建立这个文件夹
                  NSString *directryPath = [docDir_Str stringByAppendingPathComponent:@"localFile"];
                  [fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil];
              }
            

              //提醒视图: UIActionSheet
              UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"是否需要保存文档?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确认" otherButtonTitles:nil];
              [actionSheet showInView:self.window.rootViewController.view];


              //在NSUserDefaults里:保存“从文档进入App”为YES
              [User_Def setBool:YES forKey:JoinAPPFromDocumVC]; 
              [User_Def synchronize];
            
              //跳转,查看文档
              UIDocumentInteractionController *documentController =  [UIDocumentInteractionController interactionControllerWithURL:url];
              documentController.delegate = self;
              [documentController presentPreviewAnimated:YES];//跳转到查看页面
          }
     }

}


3.文档查看UIDocumentInteractionControllerDelegate

#pragma mark - UIDocumentInteractionControllerDelegate
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self.window.rootViewController;
}
-(UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    return self.window.rootViewController.view;
}
-(CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    return self.window.rootViewController.view.bounds;
}
//-(void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
//    NSLog(@"%@",controller);
//}
//-(void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
//    NSLog(@"%@",controller);
//}
//-(void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
//    NSLog(@"%@",controller);
//}




4.提醒视图:UIActionSheetDelegate
以及
5.保存操作

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"ACActionSheet delegate - %ld",buttonIndex);

    if (buttonIndex == 0) { //是否考虑 保存
        NSFileManager * fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir_Str = [paths objectAtIndex:0];
        NSString *pathStr = [docDir_Str stringByAppendingString:@"/localFile"];
        NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:pathStr];//遍历
    
    
        NSArray *fileList = [NSArray arrayWithArray:[fileManager contentsOfDirectoryAtPath:pathStr error:nil]];
        if (fileList.count == 0) { //个数为0
            NSString * fileNameStr = [_docUrl lastPathComponent];
            NSString * DocPathStr = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr];   //保存的路径
            NSData * data = [NSData dataWithContentsOfURL:_docUrl];//保存的数据
        
            if ([data writeToFile:DocPathStr atomically:YES]) {
                NSString * deleteDocPath = [[docDir_Str stringByAppendingPathComponent:@"Inbox"] stringByAppendingPathComponent:fileNameStr];
                [fileManager removeItemAtPath:deleteDocPath error:nil]; //删除Inbox里面的文件
            }
        } else {
            for (NSString *fileName in enumerator) {
                NSLog(@"%@",fileName);
                NSString * fileNameStr = [_docUrl lastPathComponent];
                NSString * fileName_total = [fileName componentsSeparatedByString:@" -"][0];
                NSString * fileNameStr_total = [fileNameStr componentsSeparatedByString:@" -"][0];
                if (![fileName_total isEqualToString:fileNameStr_total]) {
                    NSString * DocStr = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr];   //保存的路径
                    NSData * data = [NSData dataWithContentsOfURL:_docUrl];//保存的数据
                
                    if ([data writeToFile:DocStr atomically:YES]) {
                        NSString * deleteDocPath = [[docDir_Str stringByAppendingPathComponent:@"Inbox"] stringByAppendingPathComponent:fileNameStr];
                        [fileManager removeItemAtPath:deleteDocPath error:nil]; //删除Inbox里面的文件
                    }
                }
            }
        }
    
    } else { //不保存在本地

        //删除Inbox里面的文件
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir_Str = [paths objectAtIndex:0];
        NSString * fileNameStr = [_docUrl lastPathComponent];
        NSString * deleteDocPath = [[docDir_Str stringByAppendingPathComponent:@"Inbox"] stringByAppendingPathComponent:fileNameStr];
        [[NSFileManager defaultManager] removeItemAtPath:deleteDocPath error:nil];
    }
}




当然,最简单的 删除Inbox”里文件操作:

就是 —— 在文档展示界面消失后,再删除Inbox面的文件

实现代理方法:

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller { //结束 展示文档
   //(不保存,直接删除) 删除“Inbox”里面的文件
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *docDir_Str = [paths objectAtIndex:0];
   NSString * fileNameStr = [_docUrl lastPathComponent];//文件名
   NSString * deleteDocPath = [[docDir_Str stringByAppendingPathComponent:@"Inbox"] stringByAppendingPathComponent:fileNameStr];
   [[NSFileManager defaultManager] removeItemAtPath:deleteDocPath error:nil];

   NSLog(@"%@",controller);
}















goyohol's essay

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,222评论 30 472
  • 218.241.181.202 wxhl60 123456 192.168.10.253 wxhl66 wxhl6...
    CYC666阅读 1,459评论 0 6
  • /**ios常见的几种加密方法: 普通的加密方法是讲密码进行加密后保存到用户偏好设置( [NSUserDefaul...
    彬至睢阳阅读 3,042评论 0 7
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • 今天就本周作业的读取txt文件查找了的一些方法,如下: //读取文本内容NSError *error;NSStri...
    霏誠拜咬o阅读 652评论 0 0