通過(guò)UIDocumentInteractionController
或者是QLPreviewController
來(lái)預(yù)覽PDF等格式文件的時(shí)候,我們可以通過(guò)自帶的UIActivityViewController
把該文件共享出去或進(jìn)行打印等處理。如圖所示:
第一行的
AirDrop
是iOS7之后給用戶提供的一種在蘋果設(shè)備之間共享文件的快捷方式,類似于安卓上的藍(lán)牙無(wú)線傳輸文件。
第二行是通過(guò)文檔類型關(guān)聯(lián)技術(shù)識(shí)別的App的列表。
第三行是通過(guò)文檔關(guān)聯(lián)技術(shù)識(shí)別的Action
的列表,表示對(duì)文件可進(jìn)行的一些操作,如復(fù)制,打印,保存等。
我們知道在iOS系統(tǒng)下有一種安全體系--沙盒機(jī)制
,每個(gè)iOS應(yīng)用程序都是一個(gè)獨(dú)立的文件系統(tǒng),并且只能在自己的文件系統(tǒng)進(jìn)行操作,所以iOS系統(tǒng)下并不能像安卓一樣輕松取到其他應(yīng)用程序下的文件。
既然我們能在自己的應(yīng)用程序下預(yù)覽文件時(shí)把文件共享到其他App中,那么反過(guò)來(lái)怎么能讓其他App共享文件到我們App呢?
info.plist注冊(cè)文件類型
我們需要在info.plist
文件中,添加一個(gè)新的屬性CFBundleDocumentTypes
(實(shí)際上輸入的是Document type
),這是一個(gè)數(shù)組類型的屬性,意思就是我們可以同時(shí)注冊(cè)多個(gè)類型。而針對(duì)數(shù)組中的每一個(gè)元素,都有許多屬性可以指定,詳細(xì)的屬性列表我們可以從官方文檔上找到: Core Foundation Keys ---- CFBundleDocumentTypes。這里列舉我們?cè)谧鰅OS開發(fā)時(shí)常用的屬性:
-
CFBundleTypeName
字符串類型,指定某種類型的別名,也就是用來(lái)指代我們規(guī)定的類型的別稱,一般為了保持唯一性,我們使用UTI來(lái)標(biāo)識(shí)。
CFBundleTypeIconFiles
數(shù)組類型,包含指定的png圖標(biāo)的文件名,指定代表某種類型的圖標(biāo),而圖標(biāo)有具體的尺寸標(biāo)識(shí):
Device | Sizes |
---|---|
iPad | 64 x 64 pixels, 320 x 320 pixels |
iPhone and iPod touch | 22 x 29 pixels, 44 x 58 pixels (high resolution) |
- LSItemContentTypes
數(shù)組類型,包含UTI字符串,指定我們的應(yīng)用程序所有可以識(shí)別的文件類型集合
- LSHandlerRank
字符串類型,包含Owner
,Default
,Alternate
,None
四個(gè)可選值,指定對(duì)于某種類型的優(yōu)先權(quán)級(jí)別,而Launcher Service
會(huì)根據(jù)這個(gè)優(yōu)先級(jí)別來(lái)排列顯示的App的順序。優(yōu)先級(jí)別從高到低依次是Owner
,Alternate
,Default
。None
表示不接受這種類型。
我們選擇Source code
方式打開info.plist
文件添加以下代碼:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Microsoft Word</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.word.doc</string>
<string>com.microsoft.word.wordml</string>
<string>org.openxmlformats.wordprocessingml.document</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Microsoft Excel</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.excel.xls</string>
<string>org.openxmlformats.spreadsheetml.sheet</string>
</array>
</dict>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>Microsoft PowerPoint</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.powerpoint.?ppt</string>
<string>org.openxmlformats.presentationml.presentation</string>
<string>public.presentation</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Text</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.text</string>
<string>public.plain-text</string>
<string>public.utf8-plain-text</string>
<string>public.utf16-external-plain-?text</string>
<string>public.utf16-plain-text</string>
<string>com.apple.traditional-mac-?plain-text</string>
<string>public.source-code</string>
<string>public.c-source</string>
<string>public.objective-c-source</string>
<string>public.c-plus-plus-source</string>
<string>public.objective-c-plus-?plus-source</string>
<string>public.c-header</string>
<string>public.c-plus-plus-header</string>
<string>com.sun.java-source</string>
<string>public.script</string>
<string>public.shell-script</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Rich Text</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.rtf</string>
<string>com.apple.rtfd</string>
<string>com.apple.flat-rtfd</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>HTML</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.html</string>
<string>public.xhtml</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Web Archive</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.webarchive</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Image</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.image</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>iWork Pages</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.page.pages</string>
<string>com.apple.iwork.pages.pages</string>
<string>com.apple.iwork.pages.template</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>iWork Numbers</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.numbers.numbers</string>
<string>com.apple.iwork.numbers.numbers</string>
<string>com.apple.iwork.numbers.template</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>iWork Keynote</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.keynote.key</string>
<string>com.apple.iwork.keynote.key</string>
<string>com.apple.iwork.keynote.kth</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Audio</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.audio</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Movie</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.movie</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Archive</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.archive</string>
</array>
</dict>
</array>
添加這些代碼,你的App就可以支持大部分文件類型了,可以根據(jù)自己項(xiàng)目的需求,添加相關(guān)類型的代碼,像我自己的項(xiàng)目只需要支持PDF和word格式的文件。添加完這些代碼,我們選擇Property list
打開info.plist文件:
或者在info
頁(yè)面打開Document types
列表:
這個(gè)時(shí)候就代表我們已經(jīng)成功的注冊(cè)好了App支持的文件類型,這個(gè)時(shí)候我們?cè)诰幾g運(yùn)行,然后再到其他App(我這邊用的QQ)打開下載好的文件,這個(gè)時(shí)候出來(lái)的頁(yè)面是這樣的:
我們可以看到自己的App圖標(biāo)已經(jīng)出現(xiàn)在第二欄的列表中,這個(gè)時(shí)候我們點(diǎn)擊圖標(biāo)按鈕即可把文件共享到自己App中。
如何處理共享文件
當(dāng)點(diǎn)擊圖標(biāo)按鈕的時(shí)候,會(huì)跳轉(zhuǎn)到我們自己的應(yīng)用程序中,這個(gè)時(shí)候在AppDelegate.m
會(huì)走- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
該回調(diào)方法。
但是在iOS9之前回調(diào)的是- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
,所以我們需要針對(duì)不同的設(shè)備版本做出改變。
我們可以在回調(diào)方法里進(jìn)行文件處理操作,如將文件上傳、文件預(yù)覽、文件保存一些工作。在做文件預(yù)覽的時(shí)候我們必定得跳轉(zhuǎn)到對(duì)應(yīng)的控制器中,這個(gè)時(shí)候我們首先得獲取到當(dāng)前的視圖控制器
//獲取當(dāng)前屏幕顯示的viewcontroller
- (UIViewController *)getCurrentVC
{
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
return currentVC;
}
- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
UIViewController *currentVC;
if ([rootVC presentedViewController]) {
// 視圖是被presented出來(lái)的
rootVC = [rootVC presentedViewController];
}
if ([rootVC isKindOfClass:[UITabBarController class]]) {
// 根視圖為UITabBarController
currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
} else if ([rootVC isKindOfClass:[UINavigationController class]]){
// 根視圖為UINavigationController
currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
} else {
// 根視圖為非導(dǎo)航類
currentVC = rootVC;
}
return currentVC;
}
拿到控制器我們可以回到回調(diào)方法里進(jìn)行跳轉(zhuǎn)工作,我這邊還是用UIDocumentInteractionController
做文件預(yù)覽
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
// 判斷傳過(guò)來(lái)的url是否為文件類型
if ([url.scheme isEqualToString:@"file"]) {
_docVc = [UIDocumentInteractionController interactionControllerWithURL:url];
_docVc.delegate = self;
[_docVc presentPreviewAnimated:YES];
}
}
#else
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
// 判斷傳過(guò)來(lái)的url是否為文件類型
if ([url.scheme isEqualToString:@"file"]) {
_docVc = [UIDocumentInteractionController interactionControllerWithURL:url];
_docVc.delegate = self;
[_docVc presentPreviewAnimated:YES];
}
return YES;
}
#endif
#pragma mark -- UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
// 返回當(dāng)前控制器
return [self getCurrentVC];
}
參考文章 :詳解蘋果提供的UTI(統(tǒng)一類型標(biāo)識(shí)符)
本文Demo示例:WYShareFile
如果發(fā)現(xiàn)本文錯(cuò)誤,還請(qǐng)指出!謝謝?。?/p>