iOS通過UIDocumentInteractionController實現(xiàn)應用間傳文件
引言
話開篇:由于iOS沙盒機制,APP文件存儲位置只能當前應用訪問,這里簡單記錄一下用 UIDocumentInteractionController 實現(xiàn)APP間傳文件。
一、實現(xiàn)效果

兩個 APP ,TestProjectA 將文件通過 UIDocumentInteractionController 來傳遞到 TestProjectB
二、配置工程
要想通過系統(tǒng) UIDocumentInteractionController 功能展示指定的APP,那么,需要在指定的工程 Info.plist 加入如下信息:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version="1.0" >
<dict>
<key> CFBundleDocumentTypes </key>
<array>
<dict>
<key> LSHandlerRank </key>
<string> Default </string>
<key> LSItemContentTypes </key>
<array>
<string> com.adobe.pdf </string>
<string> public.data </string>
<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>
</array>
</dict>
</array>
</dict>
</plist>
三、用法
1、彈出文件其他打開方式工具欄
APP-A
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileUrl]; self.documentInteractionController.delegate = self; [self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
2、接收文件
APP-B
其實這里的所說的 "接收文件" 是有些不妥的,因為,當 AppDelegate 的方法里獲取到文件的沙盒路徑已經(jīng)是 APP-B 的了,這里只是拿來就用。
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
if ([url.scheme isEqualToString:@"file"]) {
NSString * replaceStr;
#if TARGET_IPHONE_SIMULATOR//模擬器
replaceStr = @"file://";
#elif TARGET_OS_IPHONE//真機
replaceStr = @"file:///private";
#endif
NSString * filePathStr = [[NSString stringWithFormat:@"%@",url] stringByReplacingOccurrencesOfString:replaceStr withString:@""];
/** 業(yè)務邏輯 **/
}
return YES;
}
內(nèi)容僅為簡單記錄,并不是什么新的技術(shù)。只是在開發(fā)的時候需要時權(quán)當個筆記。
以上就是iOS通過UIDocumentInteractionController實現(xiàn)應用間傳文件的詳細內(nèi)容,更多關(guān)于iOS應用間傳文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
安裝win10+黑蘋果雙系統(tǒng)零基礎(chǔ)教程(圖文)
這篇文章主要介紹了安裝win10+黑蘋果雙系統(tǒng)零基礎(chǔ)教程(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2020-01-01
iOS中關(guān)于UIWindow和statusbar的設(shè)置問題
最近在做開發(fā)時要做一個類似于UIAlertView的控件,做法是創(chuàng)建一個基于UIView的類,在里面進行自定義控件的設(shè)置,為了盡量模仿UIAlertView,在這個類里面創(chuàng)建了一個新的UIWindow并將self顯示到這個window上2017-03-03
OC runtime學習筆記之關(guān)聯(lián)對象
這篇文章主要介紹了OC runtime學習筆記之關(guān)聯(lián)對象的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-09-09
iOS App使用GCD導致的卡頓現(xiàn)象及解決方法
這篇文章主要給大家介紹了關(guān)于iOS App使用GCD導致的卡頓現(xiàn)象及解決方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07

