iOS將檔案在自己App中開啟
阿新 • • 發佈:2019-02-02
1、首先在自己App的info.plist中註冊可用型別
<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>
.pptx: org.openxmlformats.presentationml.presentation
.docx: org.openxmlformats.wordprocessingml.document
.xlsx: org.openxmlformats.spreadsheetml.sheet
新增完後,儲存,執行程式,在QQ中選擇一個檔案,選擇用其他應用開啟,就會發現我們的app
點選之後,會調轉到自己的應用,並呼叫以下方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation;
通過返回的URL可知,分享過來的檔案儲存在了Document/Inbox資料夾下
之後就可以預覽我們分享過來的檔案了。
獲取所有檔案
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Inbox"];
NSFileManager *fileManager=[NSFileManager defaultManager];
self.sourceArr = [fileManager subpathsAtPath:path];
點選進行預覽
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Inbox"] stringByAppendingPathComponent:self.sourceArr[indexPath.row]];
NSURL *url = [NSURL fileURLWithPath:path];
QLPreviewController *vc = [QLPreviewController new];
self.filePath = url;
vc.delegate = self;
vc.dataSource = self;
[self presentViewController:vc animated:YES completion:nil];
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
return 1;
}
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
return self.filePath;
}