UIDocumentInteractionController是從iOS 3.2的SDK開始支持的阁最,它是直接繼承的NSObject,而不是我們想象的UIViewController弥臼。
基礎(chǔ)操作
在當前“視圖控制器”里面:
NSString *cachePath = [[NSBundle mainBundle] pathForResource:@"民族" ofType:@"xlsx"];
NSURL * pathUrl = [NSURL fileURLWithPath:cachePath];//文件路徑
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:pathUrl];
documentController.delegate = self;
[documentController presentPreviewAnimated:YES];//跳轉(zhuǎn)到查看頁面
#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; //跳轉(zhuǎn)到文檔查看頁面 [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> <!--所關(guān)聯(lián)的UTI格式兆旬,一個類型名可以對應多個UTI--> <array> <string>com.adobe.pdf</string> </array> <key>LSHandlerRank</key> <!--是不是app所擁有的類型假抄,Default:不是,Owner:是--> <string>Owner</string> </dict> </array>
在以下鏈接中丽猬,有文件后綴 和 UTI(統(tǒng)一類型標識符) 的對應表:
https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html“info.plist”修改實例:(“Property List”格式)
如何導入文檔:
當我們從其他的App宿饱,用“readerApp”打開文檔時,會調(diào)用“readerApp”的方法脚祟!
“- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { }
”
其中谬以,url對應的就是該文檔的位置。
實際在此時由桌,該文檔已經(jīng)復制到“readerApp”的“document/inbox”文件夾下为黎。
我們需要將該文檔移動到其他位置,因為每次其他應用調(diào)用“readerApp”打開該文檔時行您,都會在inbox文件夾下復制一份文件(不考慮該文件是否已經(jīng)存在)铭乾,如果多次使用,那么inbox將會占用大量的存儲空間娃循。
總結(jié):記得在移動了文檔后炕檩,要刪除掉“inbox”路徑里面的內(nèi)容!
自己的文檔閱讀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”格式:
添加可支持的類型之前 | 添加可支持的類型之后 | |
---|---|---|
反復導入文檔:
每次導入揭朝,都會在inbox文件夾下復制一份文件队贱。
所以出現(xiàn)了:名稱后面文檔 命名為“-3”的情況
解決的操作
導入文檔后,不管是否保存在本地潭袱,都要刪除Inbox 里面的文件:
//fileNameStr:文件名 (導入所產(chǎn)生文檔所在的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];
全局變量: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];
//跳轉(zhuǎn)屯换,查看文檔
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
documentController.delegate = self;
[documentController presentPreviewAnimated:YES];//跳轉(zhuǎn)到查看頁面
}
}
}
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) { //個數(shù)為0
NSString * fileNameStr = [_docUrl lastPathComponent];
NSString * DocPathStr = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr]; //保存的路徑
NSData * data = [NSData dataWithContentsOfURL:_docUrl];//保存的數(shù)據(jù)
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];//保存的數(shù)據(jù)
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”里面的文件。
實現(xiàn)代理方法:
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller { //結(jié)束 展示文檔 //(不保存嘉抓,直接刪除) 刪除“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); }