A document picker view controller is used to access the files from variety of the sources like iCloud, dropbox, google drive etc. It provides the below following main operation.
- Open
- Import
- Export
- Move
Today
we will learn how to achieve (mainly used Import and Export) this functionality
in our iOS apps for providing more option to user to perform operation with the
files.
Importing a
file using document picker
/** Create the array of
UTI Type that you want to support
* Pass the array of UTI Type that
application wants to support. Add more UTI Type if you want to support more
other than listed
*/
NSArray *types = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeSpreadsheet,(NSString*)kUTTypePresentation,(NSString*)kUTTypeDatabase,(NSString*)kUTTypeFolder,(NSString*)kUTTypeZipArchive,(NSString*)kUTTypeVideo];
//Create a object of document picker view and set the mode to Import
UIDocumentPickerViewController
*docPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeImport];
//Set the delegate
docPicker.delegate
= self;
//present the document picker
[self presentViewController:docPicker animated:YES completion:nil];
It will open the document
picker to import the file that match the given UTI Type. You can restrict the
specific file type according to your use.
Exporting a
file using document picker –
//Create the file path
of the document to upload
NSURL *filePathToUpload = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"testing" ofType:@"doc"]] ;
//Create a object of document picker view and set the mode to Export
UIDocumentPickerViewController
*docPicker = [[UIDocumentPickerViewController alloc] initWithURL:filePathToUpload
inMode:UIDocumentPickerModeExportToService];
//Set the delegate
docPicker.delegate
= self;
//present the document picker
[self presentViewController:docPicker animated:YES completion:nil];
Create the url with the
document that you want to upload and then initiate the document picker with
that url.
When the above import or
export methods are called we will get the status in the delegate methods of UIDocumentPickerDelegate.
/**
* This
delegate method is called when user will either upload or download the file.
*
*
@param controller UIDocumentPickerViewController object
*
@param url url of the file
*/
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
{
if (controller.documentPickerMode == UIDocumentPickerModeImport)
{
// Condition called when user download the file
NSData
*fileData = [NSData dataWithContentsOfURL:url];
// NSData of the content that was
downloaded - Use this to upload on the server or save locally in directory
//Showing alert for success
dispatch_async(dispatch_get_main_queue(), ^{
NSString
*alertMessage = [NSString stringWithFormat:@"Successfully downloaded file
%@", [url lastPathComponent]];
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"UIDocumentView"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
});
}else if (controller.documentPickerMode == UIDocumentPickerModeExportToService)
{
// Called when user uploaded the file - Display
success alert
dispatch_async(dispatch_get_main_queue(), ^{
NSString
*alertMessage = [NSString stringWithFormat:@"Successfully uploaded file
%@", [url lastPathComponent]];
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"UIDocumentView"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
});
}
}
/**
*
Delegate called when user cancel the document picker
*
*
@param controller - document picker object
*/
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
}
UIDocumentMenuView
Instead
of directly presenting the document picker view we can also present menu view
that contains all the document providers. Use can control which extensions they
want to display and which they don’t want.
We can also add our own option in the list either in top or bottom of the list.
We can also add our own option in the list either in top or bottom of the list.
/** Create the array of
UTI Type that you want to support
* Pass the array of UTI Type that
application wants to support. Add more UTI
Type if you want to support more
other than listed
*/
NSArray *types = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeSpreadsheet,(NSString*)kUTTypePresentation,(NSString*)kUTTypeDatabase,(NSString*)kUTTypeFolder,(NSString*)kUTTypeZipArchive,(NSString*)kUTTypeVideo];
//Create a object of document menu view and set the mode to Import
UIDocumentMenuViewController
*objMenuView = [[UIDocumentMenuViewController alloc]initWithDocumentTypes:types inMode:UIDocumentPickerModeImport];
//Create Custom option to display
[objMenuView addOptionWithTitle:@"My
Custom option" image:nil order:UIDocumentMenuOrderFirst handler:^{
//Call when user select the option
}];
//Set the delegate
objMenuView.delegate
= self;
//present the document menu view
[self presentViewController:objMenuView animated:YES completion:nil];
When user select any
option other than custom we have to display the document view controller to
select the file.To get the notification we have to implement UIDocumentMenuDelegate.
- (void)documentMenu:(UIDocumentMenuViewController *)documentMenu didPickDocumentPicker:(UIDocumentPickerViewController *)documentPicker {
documentPicker.delegate
= self;
[self presentViewController:documentPicker animated:YES completion:nil];
}
Note- To use document picker and iCloud you must enable iCloud in project settings and create the entitlement.
That’s all we have
successfully integrated the document menu view and document picker view
controller.
Here is sample project with all the code of the above tutorial.
If you face any issue or have any suggestions, please leave your comment.
If you face any issue or have any suggestions, please leave your comment.
Nice Article Pradeep :)
ReplyDeletecan we get the whole information of selected document like date ?
ReplyDeleteyou will get the url of the resource so you can not get any other information.
DeleteOk Thanks.
DeleteIs there any way to open that file url in webview in my own application?
ReplyDeleteNo, you have to download that file in document directory and then you can do whatever you want.
Deleteif i get url of the selected document then can i download that document into my application to view it ?
ReplyDeleteYou have to copy that file in your document directory and then you can use that. Please check below link for copying to doc directory.
Deletehttp://stackoverflow.com/questions/32175063/how-to-copy-files-from-a-directory-to-iphone-document-directory
Nice !!
ReplyDeleteWorking nice but google drive is not visible.I installed google drive app but when I select more option drive is being visible but instantly hiding from list.
ReplyDeleteThat should not happen as it works fine with google drive. Could you please also check the same in sample code.
DeleteIf you have toggle on google drive it will always visible in the list.
Also, please check if you have sign in on google dive or not.
plese update with a swift version
ReplyDeletehow to add imageview to option bar
ReplyDeletethanks
ReplyDeleteI have one drive installed on my iphone.
ReplyDeleteI can see one drive when I click on download option but I dont see it on upload option.
I read it somewhere like its a known issue with ios 11.Please help me out
This comment has been removed by the author.
ReplyDeleteYou can enable/ disable the options via Manage Locations.
ReplyDeleteThanks for sharing this valuable post with us.
ReplyDeleteIOS App Development Course in Delhi
hey buddy! i can't download your sample project.
ReplyDeleteis this project have in swift
ReplyDeleteUIDocumentPickerViewController and UIDocumentMenuViewController in iOS
ReplyDeleteVery interesting post...
5 Best Tips to choose the iOS App Development Company
ios app development company
adana eskort
ReplyDeleteadıyaman eskort
afyon eskort
balıkesir eskort
van eskort
istanbul eskort
düzce masöz
manisa masöz
izmit masöz
görükle masöz
Mmorpg Oyunlar
ReplyDeleteİnstagram Takipci Satın Al
TİKTOK JETON HİLESİ
tiktok jeton hilesi
Saç ekim antalya
İnstagram takipçi satin al
İnstagram takipçi satın al
metin2 pvp serverlar
TAKİPCİ SATİN AL
Congratulations on your article, it was very helpful and successful. 71767d8dabb41c789086513d1be62a06
ReplyDeletewebsite kurma
sms onay
numara onay
Thank you for your explanation, very good content. 9300f2532b254638acf2d09a47f5c30a
ReplyDeletedefine dedektörü
Thanks for your article. 726632f9f95d8b478d2e3c5b6e30ce94
ReplyDeleteevde iş imkanı
Good content. You write beautiful things.
ReplyDeletehacklink
vbet
mrbahis
sportsbet
sportsbet
korsan taksi
mrbahis
vbet
hacklink
https://andar-bahar.in look at game
ReplyDeletedijital kartvizit
ReplyDeletereferans kimliği nedir
binance referans kodu
referans kimliği nedir
bitcoin nasıl alınır
resimli magnet
46M
hatay
ReplyDeletekars
mardin
samsun
urfa
OYM
Access of file is easy work in spreadsheet. You can open, Export, import, move to different device. Modifying Metal Designs UTI types are acceptable.
ReplyDeletekıbrıs
ReplyDeleteniğde
tunceli
diyarbakır
uşak
UJ1JCC
adapazarı
ReplyDeleteadıyaman
afyon
alsancak
antakya
DCPYR
https://istanbulolala.biz/
ReplyDeleteWGJA
yalova evden eve nakliyat
ReplyDeletetunceli evden eve nakliyat
giresun evden eve nakliyat
ağrı evden eve nakliyat
van evden eve nakliyat
C2ADU1
urfa evden eve nakliyat
ReplyDeletemalatya evden eve nakliyat
burdur evden eve nakliyat
kırıkkale evden eve nakliyat
kars evden eve nakliyat
FASOL
BEB40
ReplyDeleteBursa Parça Eşya Taşıma
Balıkesir Lojistik
Kırşehir Evden Eve Nakliyat
Bursa Lojistik
Osmaniye Evden Eve Nakliyat
Çerkezköy Çilingir
Ünye Fayans Ustası
Tokat Evden Eve Nakliyat
Bayburt Evden Eve Nakliyat
D6A75
ReplyDeleteNevşehir Şehir İçi Nakliyat
Muş Şehirler Arası Nakliyat
Muğla Parça Eşya Taşıma
Hakkari Parça Eşya Taşıma
Bartın Şehir İçi Nakliyat
Çerkezköy Kurtarıcı
İzmir Parça Eşya Taşıma
Ağrı Parça Eşya Taşıma
Balıkesir Parça Eşya Taşıma
48110
ReplyDeleteBartın Lojistik
Poloniex Güvenilir mi
Aydın Evden Eve Nakliyat
Kilis Şehir İçi Nakliyat
Karabük Şehirler Arası Nakliyat
Ardahan Şehirler Arası Nakliyat
Balıkesir Lojistik
Keçiören Parke Ustası
Eskişehir Parça Eşya Taşıma
BACBF
ReplyDeleteRize Evden Eve Nakliyat
Silivri Parke Ustası
AAX Güvenilir mi
Kars Evden Eve Nakliyat
Çerkezköy Sineklik
Gümüşhane Evden Eve Nakliyat
Karapürçek Parke Ustası
Sakarya Evden Eve Nakliyat
Referans Kimliği Nedir
Thanks for shareing comments
ReplyDelete1E3C2
ReplyDeleteresimli magnet
referans kimliği nedir
binance referans kodu
binance referans kodu
referans kimliği nedir
resimli magnet
binance referans kodu
binance referans kodu
resimli magnet
EA029
ReplyDeletebalıkesir mobil sohbet odaları
karaman sesli sohbet odası
uşak en iyi ücretsiz görüntülü sohbet siteleri
edirne ücretsiz sohbet uygulamaları
nevşehir kadınlarla ücretsiz sohbet
sivas canli sohbet bedava
parasız sohbet
bayburt ücretsiz sohbet siteleri
çankırı mobil sesli sohbet
99753
ReplyDeletehuobi
kripto para telegram grupları
bybit
probit
bybit
filtre kağıdı
kraken
papaya meyvesi
binance
5D492
ReplyDeleteparibu
binance referans kimliği nedir
bkex
gate io
kızlarla canlı sohbet
kucoin
mercatox
kripto telegram
okex
Thanks for sharing all this information here on this page.
ReplyDeleteI truly enjoy looking through on this web site , it holds superb content .
ReplyDeleteAttract Group provides top-notch Flutter app development services for businesses looking to create dynamic and user-friendly mobile applications. Their team of experts is skilled in utilizing the Flutter framework to build high-quality apps that are compatible across different platforms. With a focus on creativity and innovation, Attract Group delivers customized solutions tailored to meet each client's specific needs. Their commitment to quality and customer satisfaction makes them a reliable choice for any company seeking professional app development services.
ReplyDeletehgfjmygujyukiuyik
ReplyDeleteتسليك مجاري بالقطيف
نفخ المجاري بالاحساء RGccLO1zx2
ReplyDelete